<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">
<script>
    (function () {
        if ('') {
            if (prompt('请输入文章密码') !== '') {
                alert('密码错误！');
                if (history.length === 1) {
                    location.replace("https://google.com"); // 这里替换成你的首页
                } else {
                    history.back();
                }
            }
        }
    })();
</script>








<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="python,">










<meta name="description" content="5 行代码入门 Python 爬虫123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384import requestsimport pa">
<meta name="keywords" content="python">
<meta property="og:type" content="article">
<meta property="og:title" content="python代码段收集">
<meta property="og:url" content="http://yoursite.com/2019/02/26/python代码段收集/index.html">
<meta property="og:site_name" content="苏生不惑的博客">
<meta property="og:description" content="5 行代码入门 Python 爬虫123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384import requestsimport pa">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-04-08T09:24:11.237Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="python代码段收集">
<meta name="twitter:description" content="5 行代码入门 Python 爬虫123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384import requestsimport pa">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/02/26/python代码段收集/">



<meta name="referrer" content="never"> ​​​​


  <title>python代码段收集 | 苏生不惑的博客</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">苏生不惑的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/02/26/python代码段收集/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">python代码段收集</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-02-26T19:45:56+08:00">
                2019-02-26
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  15.8k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  82 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="5-行代码入门-Python-爬虫"><a href="#5-行代码入门-Python-爬虫" class="headerlink" title="5 行代码入门 Python 爬虫"></a>5 行代码入门 Python 爬虫</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line"><span class="keyword">from</span> lxml <span class="keyword">import</span> etree</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> pymysql</span><br><span class="line"><span class="keyword">from</span> sqlalchemy <span class="keyword">import</span> create_engine</span><br><span class="line">from urllib.parse import urlencode  # 编码 URL 字符串https://www.makcyun.top/web_scraping_withpython18.html</span><br><span class="line"></span><br><span class="line">start_time = time.time()  #计算程序运行时间</span><br><span class="line">def get_one_page(i):</span><br><span class="line">	<span class="keyword">try</span>:</span><br><span class="line">		headers = &#123;</span><br><span class="line">            <span class="string">'User-Agent'</span>: <span class="string">'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'</span></span><br><span class="line">        &#125;</span><br><span class="line">		paras = &#123;</span><br><span class="line">		<span class="string">'reportTime'</span>: <span class="string">'2017-12-31'</span>,</span><br><span class="line">		#可以改报告日期，比如2018-6-30获得的就是该季度的信息</span><br><span class="line">		'pageNum': i   #页码</span><br><span class="line">		&#125;</span><br><span class="line">		url = <span class="string">'http://s.askci.com/stock/a/?'</span> + urlencode(paras)</span><br><span class="line">		response = requests.get(url,headers = headers)</span><br><span class="line">		<span class="keyword">if</span> response.status_code == <span class="number">200</span>:</span><br><span class="line">			<span class="keyword">return</span> response.text</span><br><span class="line">		<span class="keyword">return</span> None</span><br><span class="line">	except RequestException:</span><br><span class="line">		print(<span class="string">'爬取失败'</span>)</span><br><span class="line"></span><br><span class="line">def parse_one_page(html):</span><br><span class="line">	soup = BeautifulSoup(html,<span class="string">'lxml'</span>)</span><br><span class="line">	content = soup.select('#myTable04')[0] #[0]将返回的list改为bs4类型</span><br><span class="line">	tbl = pd.read_html(content.prettify(),header = <span class="number">0</span>)[<span class="number">0</span>]</span><br><span class="line">	# prettify()优化代码,[0]从pd.read_html返回的list中提取出DataFrame</span><br><span class="line">	tbl.rename(columns = &#123;<span class="string">'序号'</span>:<span class="string">'serial_number'</span>, <span class="string">'股票代码'</span>:<span class="string">'stock_code'</span>, <span class="string">'股票简称'</span>:<span class="string">'stock_abbre'</span>, <span class="string">'公司名称'</span>:<span class="string">'company_name'</span>, <span class="string">'省份'</span>:<span class="string">'province'</span>, <span class="string">'城市'</span>:<span class="string">'city'</span>, <span class="string">'主营业务收入(201712)'</span>:<span class="string">'main_bussiness_income'</span>, <span class="string">'净利润(201712)'</span>:<span class="string">'net_profit'</span>, <span class="string">'员工人数'</span>:<span class="string">'employees'</span>, <span class="string">'上市日期'</span>:<span class="string">'listing_date'</span>, <span class="string">'招股书'</span>:<span class="string">'zhaogushu'</span>, <span class="string">'公司财报'</span>:<span class="string">'financial_report'</span>, <span class="string">'行业分类'</span>:<span class="string">'industry_classification'</span>, <span class="string">'产品类型'</span>:<span class="string">'industry_type'</span>, <span class="string">'主营业务'</span>:<span class="string">'main_business'</span>&#125;,inplace = True)</span><br><span class="line">	<span class="keyword">return</span> tbl</span><br><span class="line"></span><br><span class="line">def generate_mysql():</span><br><span class="line">	conn = pymysql.connect(</span><br><span class="line">		host=<span class="string">'localhost'</span>,</span><br><span class="line">		user=<span class="string">'root'</span>,</span><br><span class="line">		password=<span class="string">'******'</span>,</span><br><span class="line">		port=<span class="number">3306</span>,</span><br><span class="line">		charset = <span class="string">'utf8'</span>,  </span><br><span class="line">		db = <span class="string">'wade'</span>)</span><br><span class="line">	cursor = conn.cursor()</span><br><span class="line"></span><br><span class="line">	sql = <span class="string">'CREATE TABLE IF NOT EXISTS listed_company (serial_number INT(20) NOT NULL,stock_code INT(20) ,stock_abbre VARCHAR(20) ,company_name VARCHAR(20) ,province VARCHAR(20) ,city VARCHAR(20) ,main_bussiness_income VARCHAR(20) ,net_profit VARCHAR(20) ,employees INT(20) ,listing_date DATETIME(0) ,zhaogushu VARCHAR(20) ,financial_report VARCHAR(20) , industry_classification VARCHAR(20) ,industry_type VARCHAR(100) ,main_business VARCHAR(200) ,PRIMARY KEY (serial_number))'</span></span><br><span class="line">	cursor.execute(sql)</span><br><span class="line">	conn.close()</span><br><span class="line">	</span><br><span class="line">def write_to_sql(tbl, db = <span class="string">'wade'</span>):</span><br><span class="line">    engine = create_engine(<span class="string">'mysql+pymysql://root:******@localhost:3306/&#123;0&#125;?charset=utf8'</span>.format(db))</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">    	tbl.to_sql(<span class="string">'listed_company2'</span>,con = engine,if_exists=<span class="string">'append'</span>,index=False)</span><br><span class="line">    	# append表示在原有表基础上增加，但该表要有表头</span><br><span class="line">    except Exception <span class="keyword">as</span> e:</span><br><span class="line">    	print(e)</span><br><span class="line"></span><br><span class="line">def main(page):</span><br><span class="line">    generate_mysql()</span><br><span class="line">	<span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">1</span>,page):  </span><br><span class="line">		html = get_one_page(i)</span><br><span class="line">		tbl = parse_one_page(html)</span><br><span class="line">		write_to_sql(tbl)</span><br><span class="line">		</span><br><span class="line"># # 单进程</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:	</span><br><span class="line">	main(<span class="number">178</span>)</span><br><span class="line">	endtime = time.time()-start_time</span><br><span class="line">	print(<span class="string">'程序运行了%.2f秒'</span> %endtime)</span><br><span class="line">	</span><br><span class="line"># 多进程</span><br><span class="line"><span class="keyword">from</span> multiprocessing <span class="keyword">import</span> Pool</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> 	pool = Pool(<span class="number">4</span>)</span><br><span class="line"> 	pool.map(main, [i for i in range(1,178)])  #共有178页</span><br><span class="line">	endtime = time.time()-start_time</span><br><span class="line">	print(<span class="string">'程序运行了%.2f秒'</span> %(time.time()-start_time))</span><br><span class="line">	</span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> csv</span><br><span class="line">for i in range(1,178):  # 爬取全部页</span><br><span class="line">	tb = pd.read_html(<span class="string">'http://s.askci.com/stock/a/?reportTime=2017-12-31&amp;pageNum=%s'</span> % (str(i)))[<span class="number">3</span>] </span><br><span class="line">	tb.to_csv(r<span class="string">'1.csv'</span>, mode=<span class="string">'a'</span>, encoding=<span class="string">'utf_8_sig'</span>, header=<span class="number">1</span>, index=<span class="number">0</span>)</span><br></pre></td></tr></table></figure>
<h3 id="时间处理"><a href="#时间处理" class="headerlink" title="时间处理"></a>时间处理</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br></pre></td><td class="code"><pre><span class="line"># -*- coding: utf-8 -*-</span><br><span class="line"># author:           inpurer(月小水长)</span><br><span class="line"># pc_type           lenovo</span><br><span class="line"># create_date:      2018/12/3</span><br><span class="line"># file_name:        timetest.py</span><br><span class="line"># description:      月小水长，热血未凉</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line">t0 = time.time()</span><br><span class="line">#description:   输出当前时间距离1970.1.1的秒数,精确到小数点后6位，也叫做时间戳</span><br><span class="line">#output sample: 1543799532.602318</span><br><span class="line">print(t0)</span><br><span class="line"></span><br><span class="line">t1 = time.localtime(t0)</span><br><span class="line">#description:   把时间戳转成元组,包含七个元素,前六个见名知意,tm_wday是指今天是当前周的第几天(index from 0),tm_yday类似,tm_isdst是否是夏令时,不用关心</span><br><span class="line">#output sample: time.struct_time(tm_year=2018, tm_mon=12, tm_mday=3, tm_hour=9, tm_min=22, tm_sec=24, tm_wday=0, tm_yday=337, tm_isdst=0)</span><br><span class="line">print(t1)</span><br><span class="line">#so,可以这样输出今天是今年的第多少天</span><br><span class="line">print(t1[<span class="number">-2</span>]+<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#下面是对该元组的格式化</span><br><span class="line"></span><br><span class="line">#description:   简单可读形式</span><br><span class="line">#output sample: Mon Dec  3 09:31:18 2018</span><br><span class="line">t2 = time.asctime(t1)</span><br><span class="line">print(t2)</span><br><span class="line"></span><br><span class="line">#description:   可通过参数设置成各种形式，下面是一种标准形式,各参数见名知意</span><br><span class="line">#output sample: 2018-12-03 09:33:36</span><br><span class="line">t3 = time.strftime(<span class="string">"%Y-%m-%d %H:%M:%S"</span>, t1)</span><br><span class="line">print(t3)</span><br><span class="line">#%y 两位数的年份表示（00-99）</span><br><span class="line"># %Y 四位数的年份表示（000-9999）</span><br><span class="line"># %m 月份（01-12）</span><br><span class="line"># %d 月内中的一天（0-31）</span><br><span class="line"># %H 24小时制小时数（0-23）</span><br><span class="line"># %I 12小时制小时数（01-12）</span><br><span class="line"># %M 分钟数（00=59）</span><br><span class="line"># %S 秒（00-59）</span><br><span class="line">#</span><br><span class="line"># %a 本地简化星期名称</span><br><span class="line"># %A 本地完整星期名称</span><br><span class="line"># %b 本地简化的月份名称</span><br><span class="line"># %B 本地完整的月份名称</span><br><span class="line"># %c 本地相应的日期表示和时间表示</span><br><span class="line"># %j 年内的一天（001-366）</span><br><span class="line"># %p 本地A.M.或P.M.的等价符</span><br><span class="line"># %U 一年中的星期数（00-53）星期天为星期的开始</span><br><span class="line"># %w 星期（0-6），星期天为星期的开始</span><br><span class="line"># %W 一年中的星期数（00-53）星期一为星期的开始</span><br><span class="line"># %x 本地相应的日期表示</span><br><span class="line"># %X 本地相应的时间表示</span><br><span class="line"># %Z 当前时区的名称</span><br><span class="line"># %% %号本身</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 下面是把格式化字符串转成元组</span><br><span class="line"># description:      第一个参数个格式化后的字符串,后一个参数和格式化对应，便于反格式化</span><br><span class="line"># output sample:    time.struct_time(tm_year=2018, tm_mon=12, tm_mday=3, tm_hour=9, tm_min=47, tm_sec=7, tm_wday=0, tm_yday=337, tm_isdst=-1)</span><br><span class="line">t4 = time.strptime(t3,<span class="string">'%Y-%m-%d %H:%M:%S'</span>)</span><br><span class="line">print(t4)</span><br><span class="line"></span><br><span class="line"># 把元组转成时间戳</span><br><span class="line">#description:   是time.localtime的反函数,不过由于格式化的原因，精度有所下降</span><br><span class="line">#output sample: 1543801627.0</span><br><span class="line">t5 = time.mktime(t4)</span><br><span class="line">print(t5)</span><br><span class="line"></span><br><span class="line"># -*- coding: utf-8 -*-</span><br><span class="line"># author:           inpurer(月小水长)</span><br><span class="line"># pc_type           lenovo</span><br><span class="line"># create_date:      2018/12/3</span><br><span class="line"># file_name:        timetest.py</span><br><span class="line"># description:      月小水长，热血未凉</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> datetime</span><br><span class="line"></span><br><span class="line">#通过datetime.datetime.now()可以获得当前日期时间的一个实例</span><br><span class="line">#这个实例是一个datetime类对象而不是字符串</span><br><span class="line">#虽然直接打印该实例输出的是一个字符串,只是调用datetime实现的__str__方法而已</span><br><span class="line">t0 = datetime.datetime.now()</span><br><span class="line">print(t0)           #print: 2018-12-03 12:55:49.905971</span><br><span class="line">print(type(t0))     #print: &lt;class 'datetime.datetime'&gt;</span><br><span class="line"></span><br><span class="line">#然后就可以通过对象名.的方法输出各个时间信息,该信息是一个int类型</span><br><span class="line">print(t0.year)          #print: 2018</span><br><span class="line">print(type(t0.year))    #print: &lt;class 'int'&gt;</span><br><span class="line">print(t0.month)</span><br><span class="line">print(t0.day)</span><br><span class="line">print(t0.hour)</span><br><span class="line">print(t0.minute)</span><br><span class="line">print(t0.second)</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> datetime</span><br><span class="line">t0 = datetime.datetime.now()</span><br><span class="line">#注意year/month/day都是int类型，不像java那样可以直接拼接字符串和数字</span><br><span class="line">wanted_time = str(t0.year)+<span class="string">"-"</span>+str(t0.month)+<span class="string">"-"</span>+str(t0.day)</span><br><span class="line">#https://inspurer.github.io/2018/12/03/%E4%B8%80%E6%96%87%E6%90%9E%E5%AE%9Apython%E7%9A%84%E6%97%B6%E9%97%B4%E5%A4%84%E7%90%86/</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line">t0 = time.localtime()</span><br><span class="line">wanted_time = time.strftime(<span class="string">"%Y-%m-%d"</span>,t0)</span><br></pre></td></tr></table></figure>
<h3 id="error-Microsoft-Visual-C-14-0-is-required"><a href="#error-Microsoft-Visual-C-14-0-is-required" class="headerlink" title="error: Microsoft Visual C++ 14.0 is required"></a>error: Microsoft Visual C++ 14.0 is required</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">源码安装，但是没有 C++ 的编译环境</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">error: Microsoft Visual C++ <span class="number">14.0</span> is required. Get it <span class="keyword">with</span> <span class="string">"Microsoft Visual</span></span><br><span class="line"><span class="string">C++ Build Tools"</span>: http:<span class="comment">//landinghub.visualstudio.com/visual-cpp-build-tools</span></span><br><span class="line">如果是 Python <span class="number">27</span> 可以安装 Microsoft Visual C++ Compiler <span class="keyword">for</span> Python  <span class="number">2.7</span> https:<span class="comment">//www.microsoft.com/en-us/download/details.aspx?id=44266</span></span><br><span class="line">如果是 Python <span class="number">3</span> 可以安装 Visual C++ <span class="number">2015</span> Build Tools http:<span class="comment">//landinghub.visualstudio.com/visual-cpp-build-tools</span></span><br><span class="line">或者使用下载编译好的 exe 文件</span><br><span class="line">或者使用 whl 格式的包</span><br></pre></td></tr></table></figure>
<h3 id="逗号引发的悲剧"><a href="#逗号引发的悲剧" class="headerlink" title="逗号引发的悲剧"></a>逗号引发的悲剧</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; a = [</span><br><span class="line">...     <span class="string">'foo'</span></span><br><span class="line">...     <span class="string">'bar'</span>,</span><br><span class="line">...     <span class="string">'tree'</span></span><br><span class="line">... ]</span><br><span class="line">&gt;&gt;&gt;</span><br><span class="line">&gt;&gt;&gt; b = <span class="string">'foo'</span> <span class="string">'bar'</span></span><br><span class="line">&gt;&gt;&gt;</span><br><span class="line">&gt;&gt;&gt; print a</span><br><span class="line">[<span class="string">'foobar'</span>, <span class="string">'tree'</span>]</span><br><span class="line">&gt;&gt;&gt; print b</span><br><span class="line">foobar</span><br><span class="line">&gt;&gt;&gt;</span><br><span class="line">也就是说 <span class="string">'foo'</span> + <span class="string">'bar'</span> 等价于 <span class="string">'foo'</span> <span class="string">'bar'</span></span><br><span class="line"></span><br><span class="line">再来看另外一个例子，注意第二行后面的逗号</span><br><span class="line"> </span><br><span class="line">&gt;&gt;&gt; a = &#123;<span class="string">'foo'</span>: <span class="string">'bar'</span>&#125;</span><br><span class="line">&gt;&gt;&gt; b = a.get(<span class="string">'foo'</span>),</span><br><span class="line">&gt;&gt;&gt; c = a.get(<span class="string">'foo'</span>)</span><br><span class="line">&gt;&gt;&gt; print(b)</span><br><span class="line">(<span class="string">'bar'</span>,)</span><br><span class="line">&gt;&gt;&gt; print(c)</span><br><span class="line">bar</span><br><span class="line">&gt;&gt;&gt;</span><br><span class="line">本来 b 应该是一个字符串，结果硬是变成了元组。https:<span class="comment">//www.restran.net/2015/11/07/python-comma-issue/</span></span><br></pre></td></tr></table></figure>
<h3 id="Python获取Bing图片做壁纸"><a href="#Python获取Bing图片做壁纸" class="headerlink" title="Python获取Bing图片做壁纸"></a>Python获取Bing图片做壁纸</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">http:<span class="comment">//jeffyang.top/Python/python%E8%8E%B7%E5%8F%96Bing%E5%9B%BE%E7%89%87%E5%81%9A%E5%A3%81%E7%BA%B8/</span></span><br><span class="line">def get_url(day=<span class="number">0</span>):</span><br><span class="line">    url = <span class="string">"https://www.bing.com/HPImageArchive.aspx?format=js&amp;idx="</span> + str(day) + <span class="string">"&amp;n=1&amp;nc=1509675905008&amp;pid=hp&amp;video=1"</span></span><br><span class="line">    <span class="keyword">return</span> url</span><br><span class="line">def get_img(url, path=<span class="string">"D://wallpaper/"</span>):</span><br><span class="line">    isExists = os.path.exists(path)#https://github.com/JianFengY/BingSpider</span><br><span class="line">    <span class="keyword">if</span> not isExists:</span><br><span class="line">        os.makedirs(path) </span><br><span class="line">    html = requests.get(url)</span><br><span class="line">    content = html.json()</span><br><span class="line">    src = <span class="string">"https://www.bing.com"</span> + content[<span class="string">'images'</span>][<span class="number">0</span>][<span class="string">'url'</span>]</span><br><span class="line">    urlretrieve(src, path + content[<span class="string">'images'</span>][<span class="number">0</span>][<span class="string">'enddate'</span>] + <span class="string">'.jpg'</span>)</span><br><span class="line">def set_wallpaper_from_bmp(bmp_path):  </span><br><span class="line">    reg_key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, <span class="string">"Control Panel\\Desktop"</span>, <span class="number">0</span>, win32con.KEY_SET_VALUE)    </span><br><span class="line">    win32api.RegSetValueEx(reg_key, <span class="string">"WallpaperStyle"</span>, <span class="number">0</span>, win32con.REG_SZ, <span class="string">"2"</span>)  </span><br><span class="line">    win32api.RegSetValueEx(reg_key, <span class="string">"TileWallpaper"</span>, <span class="number">0</span>, win32con.REG_SZ, <span class="string">"0"</span>)  </span><br><span class="line">    win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, bmp_path, win32con.SPIF_SENDWININICHANGE)  </span><br><span class="line">def set_wallpaper(img_path):  </span><br><span class="line">    isExists = os.path.exists(img_path)</span><br><span class="line">    <span class="keyword">if</span> isExists:</span><br><span class="line">        img_dir = os.path.dirname(img_path)  </span><br><span class="line">        bmpImage = Image.open(img_path)  </span><br><span class="line">        new_bmp_path = os.path.join(img_dir, <span class="string">'wallpaper.bmp'</span>)  </span><br><span class="line">        bmpImage.save(new_bmp_path, <span class="string">"BMP"</span>)  </span><br><span class="line">        set_wallpaper_from_bmp(new_bmp_path)  </span><br><span class="line">        <span class="keyword">return</span> True</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="keyword">return</span> False</span><br></pre></td></tr></table></figure>
<h3 id="自动化测试工具from-selenium-import-webdriver"><a href="#自动化测试工具from-selenium-import-webdriver" class="headerlink" title="自动化测试工具from selenium import webdriver"></a>自动化测试工具from selenium import webdriver</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">           <span class="keyword">from</span> selenium.webdriver.common.by <span class="keyword">import</span> By</span><br><span class="line">           <span class="keyword">from</span> selenium.webdriver.common.keys <span class="keyword">import</span> Keys</span><br><span class="line">           <span class="keyword">from</span> selenium.webdriver.support <span class="keyword">import</span> expected_conditions <span class="keyword">as</span> EC</span><br><span class="line">           <span class="keyword">from</span> selenium.webdriver.support.wait <span class="keyword">import</span> WebDriverWait</span><br><span class="line">           </span><br><span class="line">           browser = webdriver.Chrome()</span><br><span class="line">           <span class="keyword">try</span>:</span><br><span class="line">               browser.get(<span class="string">'https://www.baidu.com'</span>)</span><br><span class="line">               input = browser.find_element_by_id(<span class="string">'kw'</span>)</span><br><span class="line">               input.send_keys(<span class="string">'Python'</span>)</span><br><span class="line">               input.send_keys(Keys.ENTER)</span><br><span class="line">               wait = WebDriverWait(browser, <span class="number">10</span>)</span><br><span class="line">               wait.until(EC.presence_of_element_located((By.ID, <span class="string">'content_left'</span>)))</span><br><span class="line">               print(browser.current_url)</span><br><span class="line">               print(browser.get_cookies())</span><br><span class="line">               print(browser.page_source)</span><br><span class="line">           <span class="keyword">finally</span>:</span><br><span class="line">               browser.close()</span><br><span class="line"></span><br><span class="line">browser = webdriver.Chrome()</span><br><span class="line">browser.get(<span class="string">'https://www.zhihu.com/explore'</span>)</span><br><span class="line">browser.execute_script(<span class="string">'window.scrollTo(0, document.body.scrollHeight)'</span>)</span><br><span class="line">browser.execute_script(<span class="string">'alert("To Bottom")'</span>)</span><br><span class="line"></span><br><span class="line">browser = webdriver.Chrome()</span><br><span class="line">browser.implicitly_wait(<span class="number">10</span>)</span><br><span class="line">browser.get(<span class="string">'https://www.zhihu.com/explore'</span>)</span><br><span class="line">input = browser.find_element_by_class_name(<span class="string">'zu-top-add-question'</span>)</span><br><span class="line">print(input)</span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    browser.get(<span class="string">'https://www.baidu.com'</span>)</span><br><span class="line">except TimeoutException:</span><br><span class="line">    print(<span class="string">'Time Out'</span>)</span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    browser.find_element_by_id(<span class="string">'hello'</span>)</span><br><span class="line">except NoSuchElementException:</span><br><span class="line">    print(<span class="string">'No Element'</span>)</span><br><span class="line"><span class="keyword">finally</span>:</span><br><span class="line">    browser.close()</span><br><span class="line">    </span><br><span class="line">    http:<span class="comment">//jeffyang.top/Python/%E7%88%AC%E8%99%AB/Python%E7%88%AC%E8%99%AB%E5%B8%B8%E7%94%A8%E5%BA%93selenium%E8%AF%A6%E8%A7%A3/</span></span><br></pre></td></tr></table></figure>
<h3 id="取交集，并集和差集"><a href="#取交集，并集和差集" class="headerlink" title="取交集，并集和差集"></a>取交集，并集和差集</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">a = [<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>]，b = [<span class="string">'b'</span>,<span class="string">'c'</span>,<span class="string">'d'</span>]</span><br><span class="line"></span><br><span class="line">交集:</span><br><span class="line"></span><br><span class="line">print list(set(a).intersection(set(b)))</span><br><span class="line"></span><br><span class="line">#或者</span><br><span class="line">isec = [val <span class="keyword">for</span> val <span class="keyword">in</span> a <span class="keyword">if</span> val <span class="keyword">in</span> b]</span><br><span class="line">print isec</span><br><span class="line"></span><br><span class="line">并集</span><br><span class="line">print list(set(a).union(set(b)))</span><br><span class="line">差集</span><br><span class="line">print list(set(b).difference(set(a))) # b-a</span><br><span class="line">a = [[<span class="number">1</span>,<span class="number">2</span>],[<span class="number">3</span>,<span class="number">4</span>],[<span class="number">1</span>,<span class="number">4</span>]]</span><br><span class="line">b = [x <span class="keyword">for</span> j <span class="keyword">in</span> a <span class="keyword">for</span> x <span class="keyword">in</span> j]</span><br><span class="line">print b</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">1</span>, <span class="number">4</span>]</span><br></pre></td></tr></table></figure>
<h3 id="list分割成固定长度子list"><a href="#list分割成固定长度子list" class="headerlink" title="list分割成固定长度子list"></a>list分割成固定长度子list</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">def splite_list(splist, s):</span><br><span class="line">    <span class="string">""</span><span class="string">"splite a list to sub list contain s"</span><span class="string">""</span></span><br><span class="line">    <span class="keyword">return</span> [splist[i:i + s] <span class="keyword">for</span> i <span class="keyword">in</span> range(len(splist)) <span class="keyword">if</span> i % s == <span class="number">0</span>]</span><br><span class="line"></span><br><span class="line">#test</span><br><span class="line">list1 = range(<span class="number">10</span>)</span><br><span class="line">splite_list(list1,<span class="number">2</span>)</span><br><span class="line">[[<span class="number">0</span>, <span class="number">1</span>], [<span class="number">2</span>, <span class="number">3</span>], [<span class="number">4</span>, <span class="number">5</span>], [<span class="number">6</span>, <span class="number">7</span>], [<span class="number">8</span>, <span class="number">9</span>]]</span><br></pre></td></tr></table></figure>
<h3 id="生成英文字母表"><a href="#生成英文字母表" class="headerlink" title="生成英文字母表"></a>生成英文字母表</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">小写字母表</span><br><span class="line">list(map(chr,list(range(<span class="number">97</span>, <span class="number">123</span>))))</span><br><span class="line"></span><br><span class="line">大写字母</span><br><span class="line">list(map(chr,list(range(<span class="number">65</span>, <span class="number">91</span>))))</span><br><span class="line"></span><br><span class="line">小写字母表</span><br><span class="line"><span class="keyword">import</span> string</span><br><span class="line">string.ascii_uppercase</span><br><span class="line"></span><br><span class="line">大写字母</span><br><span class="line"><span class="keyword">import</span> string</span><br><span class="line">string.ascii_lowercase</span><br></pre></td></tr></table></figure>
<h3 id="字典排序"><a href="#字典排序" class="headerlink" title="字典排序"></a>字典排序</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">按键排序</span><br><span class="line">dic = &#123;<span class="string">'a'</span>:<span class="number">11</span> , <span class="string">'b'</span>:<span class="number">5</span> , <span class="string">'c'</span>: <span class="number">7</span>&#125;</span><br><span class="line"></span><br><span class="line"># 升序排序</span><br><span class="line">sorted(dic.keys())</span><br><span class="line"></span><br><span class="line">#　降序排序</span><br><span class="line">sorted(dic.keys(), reverse=True)</span><br><span class="line"></span><br><span class="line">按值排序</span><br><span class="line">dic = &#123;<span class="string">'a'</span>:<span class="number">11</span> , <span class="string">'b'</span>:<span class="number">5</span> , <span class="string">'c'</span>: <span class="number">7</span>&#125;</span><br><span class="line"></span><br><span class="line"># 升序</span><br><span class="line">sorted(dic.items(), key = lambda x:x[<span class="number">1</span>])</span><br><span class="line"></span><br><span class="line"># 降序</span><br><span class="line">sorted(dic.items(), key = lambda x:x[<span class="number">1</span>],reverse =True)</span><br></pre></td></tr></table></figure>
<h3 id="微信公众号或网页自动导出"><a href="#微信公众号或网页自动导出" class="headerlink" title="微信公众号或网页自动导出"></a>微信公众号或网页自动导出</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//juejin.im/post/5b4cc601f265da0f5a2545a9</span></span><br><span class="line">https:<span class="comment">//github.com/MartinHan01/webpage2img</span></span><br><span class="line">pip install pillow</span><br><span class="line">init_filelist()</span><br><span class="line">    #首先初始化webdirver</span><br><span class="line">    driver = webdriver.Chrome()</span><br><span class="line">    #设置输出路径</span><br><span class="line">    dir = <span class="string">'./result'</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">for</span> item <span class="keyword">in</span> filelist:</span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            #获取图片路径，标题，以及输出路径</span><br><span class="line">            #自动滚动，并截图保存</span><br><span class="line">            pic_path,title = save_url(driver, item, dir)</span><br><span class="line">            #开始合并我们刚刚截的所有图</span><br><span class="line">            package_picture(pic_path, os.path.abspath(dir), title)</span><br><span class="line">        except Exception <span class="keyword">as</span> e :</span><br><span class="line">            print(e)</span><br><span class="line">python crop.py</span><br></pre></td></tr></table></figure>
<h3 id="批量压缩图片"><a href="#批量压缩图片" class="headerlink" title="批量压缩图片"></a>批量压缩图片</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">pip install --upgrade tinify</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> tinify</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line">tinify.key = <span class="string">'此处填入你的key'</span></span><br><span class="line">path = "xxx" # 图片存放的路径</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> dirpath, dirs, files <span class="keyword">in</span> os.walk(path):</span><br><span class="line">    <span class="keyword">for</span> file <span class="keyword">in</span> files:</span><br><span class="line">        imgpath = os.path.join(dirpath, file)</span><br><span class="line">        print(<span class="string">"compressing ..."</span>+ imgpath)</span><br><span class="line">        tinify.from_file(imgpath).to_file(imgpath)</span><br></pre></td></tr></table></figure>
<h3 id="重试"><a href="#重试" class="headerlink" title="重试"></a>重试</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> random</span><br><span class="line"><span class="keyword">from</span> tenacity <span class="keyword">import</span> retry</span><br><span class="line">@retry</span><br><span class="line">def do_something_unreliable():</span><br><span class="line">    <span class="keyword">if</span> random.randint(<span class="number">0</span>, <span class="number">10</span>) &gt; <span class="number">1</span>:</span><br><span class="line">        raise IOError(<span class="string">"Broken sauce, everything is hosed!!!111one"</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"Awesome sauce!"</span></span><br><span class="line">print(do_something_unreliable())</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> tenacity <span class="keyword">import</span> *</span><br><span class="line">@retry(stop=(stop_after_delay(<span class="number">10</span>) | stop_after_attempt(<span class="number">5</span>)), wait=wait_fixed(<span class="number">2</span>))</span><br><span class="line">def stop_after_10_s_or_5_retries():</span><br><span class="line">    print(<span class="string">"Stopping after 10 seconds or 5 retries"</span>)</span><br><span class="line">    raise Exception</span><br><span class="line">    </span><br><span class="line"> 重试<span class="number">5</span>次，每次间隔<span class="number">10</span>秒，重试前等待<span class="number">2</span>秒</span><br><span class="line">   def func():</span><br><span class="line">       pass</span><br><span class="line">   <span class="keyword">for</span> _ <span class="keyword">in</span> range(<span class="number">0</span>,<span class="number">100</span>):</span><br><span class="line">       <span class="keyword">while</span> True:</span><br><span class="line">           <span class="keyword">try</span>:</span><br><span class="line">               func()</span><br><span class="line">           except SomeSpecificException:</span><br><span class="line">               <span class="keyword">continue</span></span><br><span class="line">           <span class="keyword">break</span></span><br><span class="line"> def verify_url(url):</span><br><span class="line">     <span class="keyword">import</span> requests</span><br><span class="line">     <span class="keyword">try</span>:</span><br><span class="line">         requests.get(url, timeout=<span class="number">10</span>)</span><br><span class="line">         <span class="keyword">return</span> True</span><br><span class="line">     except requests.exceptions.ConnectTimeout:</span><br><span class="line">         <span class="keyword">return</span> False</span><br><span class="line"> def main():</span><br><span class="line">     <span class="keyword">for</span> _ <span class="keyword">in</span> range(<span class="number">5</span>):</span><br><span class="line">         <span class="keyword">try</span>:</span><br><span class="line">             <span class="keyword">if</span> verify_url(<span class="string">''</span>):</span><br><span class="line">                 <span class="keyword">return</span></span><br><span class="line">             <span class="keyword">else</span>:</span><br><span class="line">                 <span class="keyword">continue</span></span><br><span class="line">         except KeyError:</span><br><span class="line">             <span class="keyword">continue</span></span><br><span class="line"> <span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">     main()          </span><br><span class="line">    https:<span class="comment">//zhangslob.github.io/2019/01/14/Python%E9%87%8D%E8%AF%95%E7%9A%84%E5%A4%9A%E9%87%8D%E6%96%B9%E6%B3%95/</span></span><br></pre></td></tr></table></figure>
<h3 id="登录GitHub"><a href="#登录GitHub" class="headerlink" title="登录GitHub"></a>登录GitHub</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/usr/bin/env python</span></span><br><span class="line"># -*- coding: utf-8 -*-</span><br><span class="line"><span class="keyword">import</span> re</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">'Accept'</span>: <span class="string">'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'</span>,</span><br><span class="line">    <span class="string">'Accept-Encoding'</span>: <span class="string">'gzip, deflate, br'</span>,</span><br><span class="line">    <span class="string">'Accept-Language'</span>: <span class="string">'zh-CN,zh;q=0.9,en;q=0.8'</span>,</span><br><span class="line">    <span class="string">'Connection'</span>: <span class="string">'keep-alive'</span>,</span><br><span class="line">    <span class="string">'Host'</span>: <span class="string">'github.com'</span>,</span><br><span class="line">    <span class="string">'Upgrade-Insecure-Requests'</span>: <span class="string">'1'</span>,</span><br><span class="line">    <span class="string">'User-Agent'</span>: <span class="string">'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'</span></span><br><span class="line">&#125;</span><br><span class="line">s = requests.session()</span><br><span class="line">s.headers.update(headers)</span><br><span class="line">def get_token():</span><br><span class="line">    url = <span class="string">'https://github.com/login'</span></span><br><span class="line">    response = s.get(url)</span><br><span class="line">    pat = <span class="string">'name=\"authenticity_token\" value=\"(.*?)\"'</span></span><br><span class="line">    authenticity_token = re.findall(pat, response.text)[<span class="number">0</span>]</span><br><span class="line">    <span class="keyword">return</span> authenticity_token</span><br><span class="line">def login(authenticity_token, account, password):</span><br><span class="line">    payload = &#123;</span><br><span class="line">        <span class="string">'commit'</span>: <span class="string">'Sign in'</span>,</span><br><span class="line">        <span class="string">'utf8'</span>: <span class="string">'\u2713'</span>,</span><br><span class="line">        <span class="string">'authenticity_token'</span>: authenticity_token,</span><br><span class="line">        <span class="string">'login'</span>: account,</span><br><span class="line">        <span class="string">'password'</span>: password,</span><br><span class="line">    &#125;</span><br><span class="line">    url = <span class="string">'https://github.com/session'</span></span><br><span class="line">    response = s.post(url, data=payload)</span><br><span class="line">    print(response)</span><br><span class="line">    # do whatever you want</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    account, password = <span class="string">'account'</span>, <span class="string">'password'</span></span><br><span class="line">    authenticity_token = get_token()</span><br><span class="line">    login(authenticity_token, account, password)</span><br></pre></td></tr></table></figure>
<h3 id="多线程和多进程"><a href="#多线程和多进程" class="headerlink" title="多线程和多进程"></a>多线程和多进程</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> concurrent.futures</span><br><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line">URLS = [<span class="string">'http://www.foxnews.com/'</span>,</span><br><span class="line">        <span class="string">'http://www.cnn.com/'</span>,</span><br><span class="line">        <span class="string">'http://europe.wsj.com/'</span>,</span><br><span class="line">        <span class="string">'http://www.bbc.co.uk/'</span>,</span><br><span class="line">        <span class="string">'http://some-made-up-domain.com/'</span>]</span><br><span class="line"># Retrieve a single page and report the URL and contents</span><br><span class="line">def load_url(url, timeout):</span><br><span class="line">    <span class="keyword">with</span> urllib.request.urlopen(url, timeout=timeout) <span class="keyword">as</span> conn:</span><br><span class="line">        <span class="keyword">return</span> conn.read()</span><br><span class="line"># We can use a with statement to ensure threads are cleaned up promptly</span><br><span class="line"><span class="keyword">with</span> concurrent.futures.ThreadPoolExecutor(max_workers=<span class="number">5</span>) <span class="keyword">as</span> executor:</span><br><span class="line">    # Start the load operations and mark each future with its URL</span><br><span class="line">    future_to_url = &#123;executor.submit(load_url, url, <span class="number">60</span>): url <span class="keyword">for</span> url <span class="keyword">in</span> URLS&#125;</span><br><span class="line">    <span class="keyword">for</span> future <span class="keyword">in</span> concurrent.futures.as_completed(future_to_url):</span><br><span class="line">        url = future_to_url[future]</span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            data = future.result()</span><br><span class="line">        except Exception <span class="keyword">as</span> exc:</span><br><span class="line">            print(<span class="string">'%r generated an exception: %s'</span> % (url, exc))</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            print(<span class="string">'%r page is %d bytes'</span> % (url, len(data)))</span><br><span class="line">            </span><br><span class="line"> <span class="keyword">import</span> concurrent.futures</span><br><span class="line"> <span class="keyword">import</span> math</span><br><span class="line"> PRIMES = [</span><br><span class="line">     <span class="number">112272535095293</span>,</span><br><span class="line">     <span class="number">112582705942171</span>,</span><br><span class="line">     <span class="number">112272535095293</span>,</span><br><span class="line">     <span class="number">115280095190773</span>,</span><br><span class="line">     <span class="number">115797848077099</span>,</span><br><span class="line">     <span class="number">1099726899285419</span>]</span><br><span class="line"> def is_prime(n):</span><br><span class="line">     <span class="keyword">if</span> n % <span class="number">2</span> == <span class="number">0</span>:</span><br><span class="line">         <span class="keyword">return</span> False</span><br><span class="line">     sqrt_n = int(math.floor(math.sqrt(n)))</span><br><span class="line">     <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">3</span>, sqrt_n + <span class="number">1</span>, <span class="number">2</span>):</span><br><span class="line">         <span class="keyword">if</span> n % i == <span class="number">0</span>:</span><br><span class="line">             <span class="keyword">return</span> False</span><br><span class="line">     <span class="keyword">return</span> True</span><br><span class="line"> def main():</span><br><span class="line">     <span class="keyword">with</span> concurrent.futures.ProcessPoolExecutor() <span class="keyword">as</span> executor:</span><br><span class="line">         <span class="keyword">for</span> number, prime <span class="keyword">in</span> zip(PRIMES, executor.map(is_prime, PRIMES)):</span><br><span class="line">             print(<span class="string">'%d is prime: %s'</span> % (number, prime))</span><br><span class="line"> <span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">     main()</span><br><span class="line">     </span><br><span class="line">     </span><br><span class="line">     https:<span class="comment">//zhangslob.github.io/2018/07/03/%E5%BF%AB%E9%80%9F%E5%86%99%E4%B8%80%E4%B8%AA%E7%88%AC%E8%99%AB/</span></span><br></pre></td></tr></table></figure>
<h3 id="Pandas-做数据分析"><a href="#Pandas-做数据分析" class="headerlink" title="Pandas 做数据分析"></a>Pandas 做数据分析</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"></span><br><span class="line">url = (<span class="string">'https://raw.github.com/pandas-dev/pandas/master/pandas/tests/data/tips.csv'</span>)</span><br><span class="line">tips = pd.read_csv(url)</span><br><span class="line">output = tips.head()</span><br><span class="line">&gt;&gt;&gt; output</span><br><span class="line">   total_bill   tip     sex smoker  day    time  size</span><br><span class="line"><span class="number">0</span>       <span class="number">16.99</span>  <span class="number">1.01</span>  Female     No  Sun  Dinner     <span class="number">2</span></span><br><span class="line"><span class="number">1</span>       <span class="number">10.34</span>  <span class="number">1.66</span>    Male     No  Sun  Dinner     <span class="number">3</span></span><br><span class="line"><span class="number">2</span>       <span class="number">21.01</span>  <span class="number">3.50</span>    Male     No  Sun  Dinner     <span class="number">3</span></span><br><span class="line"><span class="number">3</span>       <span class="number">23.68</span>  <span class="number">3.31</span>    Male     No  Sun  Dinner     <span class="number">2</span></span><br><span class="line"><span class="number">4</span>       <span class="number">24.59</span>  <span class="number">3.61</span>  Female     No  Sun  Dinner     <span class="number">4</span></span><br><span class="line"></span><br><span class="line">sql 语句： SELECT total_bill, tip, smoker, time FROM tips LIMIT <span class="number">5</span>;。</span><br><span class="line"></span><br><span class="line">output = tips[[<span class="string">'total_bill'</span>, <span class="string">'tip'</span>, <span class="string">'smoker'</span>, <span class="string">'time'</span>]].head(<span class="number">5</span>)</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//learnku.com/articles/29825</span></span><br></pre></td></tr></table></figure>
<h3 id="多线程"><a href="#多线程" class="headerlink" title="多线程"></a>多线程</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line">线程 (Thread) 是操作系统能够进行运算调度的最小单位。它被包含在进程中，是进程中的实际运作单位。一个进程中可以并发多个线程，每条线程并行执行不同的任务。同一进程中的多个线程共享进程中的全部系统资源。</span><br><span class="line">以下演示使用多线程对一个变量值进行修改，在循环的次数不多时修改后变量的值是符合预期的，当增加循环次数后，变量最终的值并不符合预期。由此可见：线程之间资源是存在竞争的，修改同一份资源必须加互斥锁，同时需要避免死锁。</span><br><span class="line"></span><br><span class="line"># coding=utf-8</span><br><span class="line"><span class="keyword">import</span> threading</span><br><span class="line"></span><br><span class="line"># 定义一个字段。多线程执行+1操作</span><br><span class="line">balance = <span class="number">0</span></span><br><span class="line"></span><br><span class="line">def worker1():</span><br><span class="line">    global balance</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">1000</span>):</span><br><span class="line">        balance += <span class="number">1</span></span><br><span class="line">    print(<span class="string">'线程1执行完成，balance='</span>+str(balance))</span><br><span class="line"></span><br><span class="line">def worker2():</span><br><span class="line">    global balance</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">1000</span>):</span><br><span class="line">        balance += <span class="number">1</span></span><br><span class="line">    print(<span class="string">'线程2执行完成，balance='</span>+str(balance))</span><br><span class="line"></span><br><span class="line">def main():</span><br><span class="line">    # 构造线程对象</span><br><span class="line">    t1 = threading.Thread(target=worker1)</span><br><span class="line">    t2 = threading.Thread(target=worker2)</span><br><span class="line">    # 开始执行</span><br><span class="line">    t1.start()</span><br><span class="line">    t2.start()</span><br><span class="line"></span><br><span class="line">    <span class="string">""</span><span class="string">"</span></span><br><span class="line"><span class="string">    循环次数为1000时，程序输出：</span></span><br><span class="line"><span class="string">        线程1执行完成，balance=1000</span></span><br><span class="line"><span class="string">        线程2执行完成，balance=2000</span></span><br><span class="line"><span class="string">    循环次数为1000000时，程序输出：</span></span><br><span class="line"><span class="string">        线程1执行完成，balance=1180919</span></span><br><span class="line"><span class="string">        线程2执行完成，balance=1179703</span></span><br><span class="line"><span class="string">    "</span><span class="string">""</span>    </span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    main()</span><br><span class="line">要想解决以下的问题，需要使用线程的锁对象，只需要对 worker1 和 woker2 方法进行修改。</span><br><span class="line"></span><br><span class="line"># 创建一个互斥锁，默认是未锁定状态</span><br><span class="line">mutex = threading.Lock()</span><br><span class="line"></span><br><span class="line">def worker1():</span><br><span class="line">    global balance</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">1000000</span>):</span><br><span class="line">        mutex.acquire()</span><br><span class="line">        balance += <span class="number">1</span></span><br><span class="line">        mutex.release()</span><br><span class="line">    print(<span class="string">'线程1执行完成，balance='</span> + str(balance))</span><br><span class="line"></span><br><span class="line">def worker2():</span><br><span class="line">    global balance</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">1000000</span>):</span><br><span class="line">        mutex.acquire()</span><br><span class="line">        balance += <span class="number">1</span></span><br><span class="line">        mutex.release()</span><br><span class="line">    print(<span class="string">'线程2执行完成，balance='</span> + str(balance))</span><br><span class="line"></span><br><span class="line"><span class="string">""</span><span class="string">"</span></span><br><span class="line"><span class="string">加了互斥锁之后的输出：</span></span><br><span class="line"><span class="string">    线程1执行完成，balance=1941343</span></span><br><span class="line"><span class="string">    线程2执行完成，balance=2000000</span></span><br><span class="line"><span class="string">"</span><span class="string">""</span></span><br><span class="line">特点：</span><br><span class="line"></span><br><span class="line">线程执行的顺序是不确定的</span><br><span class="line">主线程【进程】会等待所有子线程结束后才会退出，主线程【进程】结束么子线程必然结束</span><br><span class="line">线程间共享资源</span><br><span class="line">修改资源必要时需要加锁，同时避免死锁</span><br><span class="line">占用的资源比进程少</span><br><span class="line">线程并不是越多越快</span><br><span class="line">由于 GIL 的原因，多线程并不是真正的并发，只是交替执行</span><br><span class="line">https:<span class="comment">//learnku.com/articles/29367</span></span><br></pre></td></tr></table></figure>
<h3 id="尾递归"><a href="#尾递归" class="headerlink" title="尾递归"></a>尾递归</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"># 例子代码</span><br><span class="line">def tail_recursion(n, total=<span class="number">0</span>):</span><br><span class="line">    <span class="keyword">if</span> n == <span class="number">0</span>:</span><br><span class="line">        <span class="keyword">return</span> total</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="keyword">return</span> tail_recursion(n<span class="number">-1</span>, total+n)</span><br><span class="line"># 执行结果：</span><br><span class="line">tail_recursion(<span class="number">5</span>)</span><br><span class="line">tail_recursion(<span class="number">4</span>, <span class="number">5</span>)</span><br><span class="line">tail_recursion(<span class="number">3</span>, <span class="number">9</span>)</span><br><span class="line">tail_recursion(<span class="number">2</span>, <span class="number">12</span>)</span><br><span class="line">tail_recursion(<span class="number">1</span>, <span class="number">14</span>)</span><br><span class="line">tail_recursion(<span class="number">0</span>, <span class="number">15</span>)</span><br><span class="line"><span class="number">5</span>+<span class="number">4</span>+<span class="number">3</span>+<span class="number">2</span>+<span class="number">1</span>=<span class="number">15</span></span><br></pre></td></tr></table></figure>
<h3 id="获取公众号全部文章"><a href="#获取公众号全部文章" class="headerlink" title="获取公众号全部文章"></a>获取公众号全部文章</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//mp.weixin.qq.com/s/nkW2sYLcdsNTYTkk-4BeLA</span></span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> json</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">from</span> pymongo <span class="keyword">import</span> MongoClient</span><br><span class="line"></span><br><span class="line">url = <span class="string">'http://mp.weixin.qq.com/mp/xxx'</span>（公众号不让添加主页链接，xxx表示profile_ext)</span><br><span class="line">#url='https://mp.weixin.qq.com/mp/profile_ext?action=getmsg&amp;__biz=MzIyMjg2ODExMA==&amp;f=json&amp;offset=21&amp;count=10&amp;is_ok=1&amp;scene=124&amp;uin=NjQ3OTQwMTAy&amp;key=a90c16d3bbfeedd04adeeda7bfc81049f486e81712f95a347e33fccfb9fe00841ec6a4d0984ce32f72fe5e8c479fd13c6680b5496cda322ab1bb2b81417a10ae277a861ad580e77cdf78edbf86212c08&amp;pass_ticket=2vonvdf3N4L67te2BCa4ZqvIs1ed2MoeBqoznvfNSL%2BeKqF4YgHUvNEWLNczZovz&amp;wxtoken=&amp;appmsg_token=1015_jLHC7BDStvidMqo9YO55XLerjoP9z6UM70Q5vw~~&amp;x5=0&amp;f=json'</span><br><span class="line"># Mongo配置</span><br><span class="line">conn = MongoClient(<span class="string">'127.0.0.1'</span>, <span class="number">27017</span>)</span><br><span class="line">db = conn.wx  #连接wx数据库，没有则自动创建</span><br><span class="line">mongo_wx = db.article  #使用article集合，没有则自动创建</span><br><span class="line"></span><br><span class="line">def get_wx_article(biz, uin, key, index=<span class="number">0</span>, count=<span class="number">10</span>):</span><br><span class="line">    offset = (index + <span class="number">1</span>) * count</span><br><span class="line">    params = &#123;</span><br><span class="line">        <span class="string">'__biz'</span>: biz,</span><br><span class="line">        <span class="string">'uin'</span>: uin,</span><br><span class="line">        <span class="string">'key'</span>: key,</span><br><span class="line">        <span class="string">'offset'</span>: offset,</span><br><span class="line">        <span class="string">'count'</span>: count,</span><br><span class="line">        <span class="string">'action'</span>: <span class="string">'getmsg'</span>,</span><br><span class="line">        <span class="string">'f'</span>: <span class="string">'json'</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    headers = &#123;</span><br><span class="line">        <span class="string">'User-Agent'</span>: <span class="string">'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    response = requests.get(url=url, params=params, headers=headers)</span><br><span class="line">    resp_json = response.json()</span><br><span class="line">    <span class="keyword">if</span> resp_json.get(<span class="string">'errmsg'</span>) == <span class="string">'ok'</span>:</span><br><span class="line">        resp_json = response.json()</span><br><span class="line">        # 是否还有分页数据， 用于判断return的值</span><br><span class="line">        can_msg_continue = resp_json[<span class="string">'can_msg_continue'</span>]</span><br><span class="line">        # 当前分页文章数</span><br><span class="line">        msg_count = resp_json[<span class="string">'msg_count'</span>]</span><br><span class="line">        general_msg_list = json.loads(resp_json[<span class="string">'general_msg_list'</span>])</span><br><span class="line">        list = general_msg_list.get(<span class="string">'list'</span>)</span><br><span class="line">        print(list, <span class="string">"**************"</span>)</span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> list:</span><br><span class="line">            app_msg_ext_info = i[<span class="string">'app_msg_ext_info'</span>]</span><br><span class="line">            # 标题</span><br><span class="line">            title = app_msg_ext_info[<span class="string">'title'</span>]</span><br><span class="line">            # 文章地址</span><br><span class="line">            content_url = app_msg_ext_info[<span class="string">'content_url'</span>]</span><br><span class="line">            # 封面图</span><br><span class="line">            cover = app_msg_ext_info[<span class="string">'cover'</span>]</span><br><span class="line"></span><br><span class="line">            # 发布时间</span><br><span class="line">            datetime = i[<span class="string">'comm_msg_info'</span>][<span class="string">'datetime'</span>]</span><br><span class="line">            datetime = time.strftime(<span class="string">"%Y-%m-%d %H:%M:%S"</span>, time.localtime(datetime))</span><br><span class="line"></span><br><span class="line">            mongo_wx.insert(&#123;</span><br><span class="line">                <span class="string">'title'</span>: title,</span><br><span class="line">                <span class="string">'content_url'</span>: content_url,</span><br><span class="line">                <span class="string">'cover'</span>: cover,</span><br><span class="line">                <span class="string">'datetime'</span>: datetime</span><br><span class="line">            &#125;)</span><br><span class="line">        <span class="keyword">if</span> can_msg_continue == <span class="number">1</span>:</span><br><span class="line">            <span class="keyword">return</span> True</span><br><span class="line">        <span class="keyword">return</span> False</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        print(<span class="string">'获取文章异常...'</span>)</span><br><span class="line">        <span class="keyword">return</span> False</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    biz = <span class="string">'Mzg4MTA2Nzg0NA=='</span></span><br><span class="line">    uin = <span class="string">'NDIyMTI5NDM1'</span></span><br><span class="line">    key = <span class="string">'20a680e825f03f1e7f38f326772e54e7dc0fd02ffba17e92730ba3f0a0329c5ed310b0bd55b3c0b1f122e5896c6261df2eaea4036ab5a5d32dbdbcb0a638f5f3605cf1821decf486bb6eb4d92d36c620'</span></span><br><span class="line">    index = <span class="number">0</span></span><br><span class="line">    <span class="keyword">while</span> <span class="number">1</span>:</span><br><span class="line">        print(f<span class="string">'开始抓取公众号第&#123;index + 1&#125; 页文章.'</span>)</span><br><span class="line">        flag = get_wx_article(biz, uin, key, index=index)</span><br><span class="line">        # 防止和谐，暂停8秒</span><br><span class="line">        time.sleep(<span class="number">8</span>)</span><br><span class="line">        index += <span class="number">1</span></span><br><span class="line">        <span class="keyword">if</span> not flag:</span><br><span class="line">            print(<span class="string">'公众号文章已全部抓取完毕，退出程序.'</span>)</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line">        print(f<span class="string">'..........准备抓取公众号第&#123;index + 1&#125; 页文章.'</span>)</span><br></pre></td></tr></table></figure>
<h3 id="PHP-与-Python-代码对比"><a href="#PHP-与-Python-代码对比" class="headerlink" title="PHP 与 Python 代码对比"></a>PHP 与 Python 代码对比</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"># -*- coding: utf-8 -*-</span><br><span class="line">n = <span class="number">0</span></span><br><span class="line">https:<span class="comment">//learnku.com/articles/30958</span></span><br><span class="line"><span class="keyword">while</span> n &lt; <span class="number">3</span>:</span><br><span class="line">    #累计次数,用于循环条件</span><br><span class="line">    n = n + <span class="number">1</span></span><br><span class="line">    #定义账号和密码</span><br><span class="line">    uname = <span class="string">'tangqingsong'</span></span><br><span class="line">    pwd = <span class="string">'123123'</span></span><br><span class="line">    #接收参数</span><br><span class="line">    username = input(<span class="string">'请输入用户名:'</span>)</span><br><span class="line">    password = input(<span class="string">'请输入密码:'</span>)</span><br><span class="line"></span><br><span class="line">    #判断用户输入的账号和密码是否正确，正确将提示成功，并且退出循环体</span><br><span class="line">    <span class="keyword">if</span> uname == username and pwd == password:</span><br><span class="line">        print (<span class="string">'恭喜你，登陆成功~'</span>)</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line">    #三次机会用完的时候,提示错误次数，并告知即将退出</span><br><span class="line">    elif n == <span class="number">3</span>:</span><br><span class="line">        print(<span class="string">'已错误'</span>, n, <span class="string">'次，即将退出...'</span>)</span><br><span class="line">    #如果在三次以内,提示还剩下几次机会</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        print(<span class="string">'抱歉，账号或密码不正确，你还有'</span>, <span class="number">3</span> - n, <span class="string">'次机会'</span>)</span><br><span class="line">        </span><br><span class="line"> $n = <span class="number">0</span>;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">while</span> ($n &lt; <span class="number">3</span>) &#123;</span><br><span class="line">     #累计次数,用于循环条件</span><br><span class="line">     $n = $n + <span class="number">1</span>;</span><br><span class="line">     #定义账号和密码</span><br><span class="line">     $uname = <span class="string">'tangqingsong'</span>;</span><br><span class="line">     $pwd = <span class="string">'123123'</span>;</span><br><span class="line">     #接收参数</span><br><span class="line">     fwrite(STDOUT, <span class="string">'请输入用户名：'</span>);</span><br><span class="line">     $username = trim(fgets(STDIN));</span><br><span class="line">     fwrite(STDOUT, <span class="string">'请输入密码：'</span>);</span><br><span class="line">     $password = trim(fgets(STDIN));</span><br><span class="line"> </span><br><span class="line">     #判断用户输入的账号和密码是否正确，正确将提示成功，并且退出循环体</span><br><span class="line">     <span class="keyword">if</span> ($uname == $username and $pwd == $password) &#123;</span><br><span class="line">         print_r(<span class="string">'恭喜你，登陆成功~'</span>);</span><br><span class="line">         <span class="keyword">break</span>;</span><br><span class="line">         #三次机会用完的时候,提示错误次数，并告知即将退出</span><br><span class="line">     &#125; elseif ($n == <span class="number">3</span>) &#123;</span><br><span class="line">         print_r(<span class="string">"已错误&#123;$n&#125;次，即将退出..."</span>);</span><br><span class="line">     &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">         #如果在三次以内,提示还剩下几次机会</span><br><span class="line">         $j = <span class="number">3</span> - $n;</span><br><span class="line">         print_r(<span class="string">"抱歉，账号或密码不正确，你还有&#123;$j&#125;次机会"</span>);</span><br><span class="line">     &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<h3 id="python2与3的编码"><a href="#python2与3的编码" class="headerlink" title="python2与3的编码"></a>python2与3的编码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br></pre></td><td class="code"><pre><span class="line">Python2有两种表示字符序列的类型，分别叫做str和Unicode，str实例包含原始的<span class="number">8</span>位值；而Unicode的实例，则包含Unicode字符。</span><br><span class="line"></span><br><span class="line">str格式本质含义是“某种编码格式”，绝大多数情况下，被引号框起来的字符串，就是str，这时的字符串编码类型，其实就是你Python文件的编码类型，比如在Windows里，默认用的是GBK编码。</span><br><span class="line">Unicode格式的含义就是“用unicode编码的字符串”。Python在进入<span class="number">2.0</span>版后正式定义了了Unicode字符串这个奇怪的特性，目的就是为了处理太多种语言编码的文本。从那时开始，Python语言中的字符串类型就分为两种：一种是传统的Python字符串（各种花样编码），另一种则是新出现的Unicode</span><br><span class="line">Python3也有两种表示字符序列的类型：bytes和str。前者的实例包含原始的<span class="number">8</span>位值，后者的实例包含Unicode字符,可以说python3的str，就是python2的Unicode</span><br><span class="line"></span><br><span class="line">str格式的定义变更为”Unicode类型的字符串“，也就是说在默认情况下，被引号框起来的字符串，是使用Unicode编码的。</span><br><span class="line">而“不是Unicode的某种编码格式”，比如UTF<span class="number">-8</span>、GBK，这些编码方式被定义为了bytes，这里的bytes和py2中的str有很多相似的地方</span><br><span class="line">我们需要编写两个辅助（helper）函数，以便在这两种情况之间转换，使得转换后的输入数据能够符合开发者的预期</span><br><span class="line"></span><br><span class="line">#在Python3中，我们需要编写接受str或bytes，并总是返回str的方法：</span><br><span class="line">def to_str(bytes_or_str):</span><br><span class="line">  <span class="keyword">if</span> isinstance(bytes_or_str, bytes):</span><br><span class="line">    value = bytes_or_str.decode(<span class="string">'utf-8'</span>)</span><br><span class="line">  <span class="keyword">else</span>:</span><br><span class="line">    value = bytes_or_str</span><br><span class="line">  return value # Instance of str</span><br><span class="line">  </span><br><span class="line">#另外，还需要编写接受str或bytes，并总是返回bytes的方法：</span><br><span class="line">def to_bytes(bytes_or_str):</span><br><span class="line">  <span class="keyword">if</span> isinstance(bytes_or_str, str):</span><br><span class="line">    value = bytes_or_str.encode(<span class="string">'utf-8)</span></span><br><span class="line"><span class="string">  else:</span></span><br><span class="line"><span class="string">    value = bytes_or_str</span></span><br><span class="line"><span class="string">  return value # Instance of bytes</span></span><br><span class="line"><span class="string">  </span></span><br><span class="line"><span class="string">#在Python2中，需要编写接受str或unicode，并总是返回unicode的方法：</span></span><br><span class="line"><span class="string">#python2</span></span><br><span class="line"><span class="string">def to_unicode(unicode_or_str):</span></span><br><span class="line"><span class="string">  if isinstance(unicode_or_str, str):</span></span><br><span class="line"><span class="string">    value = unicode_or_str.decode('</span>utf<span class="number">-8</span><span class="string">')</span></span><br><span class="line"><span class="string">  else:</span></span><br><span class="line"><span class="string">    value = unicode_or_str</span></span><br><span class="line"><span class="string">  return value # Instance of unicode</span></span><br><span class="line"><span class="string">  </span></span><br><span class="line"><span class="string">#另外，还需要编写接受str或unicode，并总是返回str的方法：</span></span><br><span class="line"><span class="string">#Python2</span></span><br><span class="line"><span class="string">def to_str(unicode_or_str):</span></span><br><span class="line"><span class="string">  if isinstance(unicode_or_str, unicode):</span></span><br><span class="line"><span class="string">    value = unicode_or_str.encode('</span>utf<span class="number">-8</span><span class="string">')</span></span><br><span class="line"><span class="string">  else:</span></span><br><span class="line"><span class="string">    value = unicode_or_str</span></span><br><span class="line"><span class="string">  reutrn vlaue # Instance of str</span></span><br><span class="line"><span class="string">  https://xin053.github.io/2016/10/30/Python%E5%AD%A6%E4%B9%A0%E9%87%8D%E7%82%B9%E6%91%98%E8%AE%B0/</span></span><br><span class="line"><span class="string">  </span></span><br><span class="line"><span class="string">   str包含一个encode方法，使用特定编码将该字符串其转换为一个bytes，这称之为编码</span></span><br><span class="line"><span class="string">  。bytes类包含了一个decode方法，也接受一个编码作为单个必要参数，并返回一个str，</span></span><br><span class="line"><span class="string">  这称之为解码。</span></span><br><span class="line"><span class="string"> </span></span><br><span class="line"><span class="string">  s='</span>n排球①’</span><br><span class="line">  b1=s.encode（<span class="string">'utf-8'</span>）</span><br><span class="line">  b2=s.encode（）</span><br><span class="line">  print（b1）</span><br><span class="line">  print（b2）</span><br><span class="line">  b\xcf\x80\xe6\x8e\x92\xe7\x9e\x83\xe3\×<span class="number">8</span>l\xae<span class="string">'</span></span><br><span class="line"><span class="string">  b'</span>\xcf\×<span class="number">80</span>\xe6\x8e\x92\xe7\x9e\x83\xe3\x8l\xae<span class="string">'</span></span><br><span class="line"><span class="string">  </span></span><br><span class="line"><span class="string">  inport sys </span></span><br><span class="line"><span class="string">  print（sys.platform）</span></span><br><span class="line"><span class="string">  print（sys.getdefaultencoding（））</span></span><br><span class="line"><span class="string">  win32utf-8</span></span><br><span class="line"><span class="string">  可以看出我这个平台默认选择的就是utf-8编码方式。</span></span><br><span class="line"><span class="string">  b=b'</span>\xe6lx8e\x92\xe7\x9e\x83<span class="string">'</span></span><br><span class="line"><span class="string">  s1=b.decode（encoding='</span>utf<span class="number">-8</span><span class="string">"）</span></span><br><span class="line"><span class="string">  s2=b.decode（）</span></span><br><span class="line"><span class="string">  s3=b.decode（encoding='latin-1'）</span></span><br><span class="line"><span class="string">  print（s1）</span></span><br><span class="line"><span class="string">  print（s2）</span></span><br><span class="line"><span class="string">  print（s3）</span></span><br><span class="line"><span class="string">  排球</span></span><br><span class="line"><span class="string">  排球</span></span><br><span class="line"><span class="string">  e2'cf</span></span><br><span class="line"><span class="string">  </span></span><br><span class="line"><span class="string">  &gt;&gt;&gt; '请'.encode('unicode-escape')</span></span><br><span class="line"><span class="string">  b'\\u8bf7'</span></span><br><span class="line"><span class="string">  &gt;&gt;&gt; b'\u8bf7'.decode('unicode-escape')</span></span><br><span class="line"><span class="string">  '请'</span></span><br><span class="line"><span class="string">  </span></span><br><span class="line"><span class="string">  s='apple'</span></span><br><span class="line"><span class="string">  b=b'apple'</span></span><br><span class="line"><span class="string">  print（b）</span></span><br><span class="line"><span class="string">  print（type（b））</span></span><br><span class="line"><span class="string">  print（s）</span></span><br><span class="line"><span class="string">  print（type（s））</span></span><br><span class="line"><span class="string">  b'apple'</span></span><br><span class="line"><span class="string">  &lt;class'bytes'&gt;</span></span><br><span class="line"><span class="string">  apple</span></span><br><span class="line"><span class="string">  &lt;class'str'&gt;</span></span><br><span class="line"><span class="string">  再近距离的看看bytes类型字节字符串，本质上它就是一串单字节16进制数b=b'apple' https://www.zhihu.com/question/35584979</span></span><br><span class="line"><span class="string">  print（b[0]）</span></span><br><span class="line"><span class="string">  print（b[1：]）</span></span><br><span class="line"><span class="string">  print（1ist（b））</span></span><br><span class="line"><span class="string">  97</span></span><br><span class="line"><span class="string">  b'pple'</span></span><br><span class="line"><span class="string">  [97，112，112，188，101]</span></span><br><span class="line"><span class="string">  </span></span><br><span class="line"><span class="string">  s=' AABec'</span></span><br><span class="line"><span class="string">  with open(' utf-8data','w', encoding=' utf-8"</span>) <span class="keyword">as</span> f: <span class="string">' +</span></span><br><span class="line"><span class="string">   '</span>f. urite(s)</span><br><span class="line">  <span class="keyword">with</span> open(<span class="string">' utf-8data'</span>,<span class="string">'r'</span>, encoding=<span class="string">' utf-8'</span>) <span class="keyword">as</span> f: </span><br><span class="line">  u_str=f. read()</span><br><span class="line">  print(u_str)</span><br><span class="line">  AABeC</span><br><span class="line">  <span class="keyword">with</span> open(<span class="string">' utf-8data",'</span> rb<span class="string">') as f: byte_str=f. read()</span></span><br><span class="line"><span class="string">  print(byte_str)</span></span><br><span class="line"><span class="string">  print(byte_str. decode(encoding='</span> utf<span class="number">-8</span>))</span><br><span class="line">  b<span class="string">'A\ XC3\X84B\ xC3\ xa8c'</span></span><br><span class="line">  AABeC</span><br></pre></td></tr></table></figure>
<h3 id="Win10-下-Python2-与-Python3-兼容问题"><a href="#Win10-下-Python2-与-Python3-兼容问题" class="headerlink" title="Win10 下 Python2 与 Python3 兼容问题"></a>Win10 下 Python2 与 Python3 兼容问题</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">新旧<span class="number">2</span>个文件，加入环境变量</span><br><span class="line">python2.bat</span><br><span class="line"></span><br><span class="line">@echo off</span><br><span class="line"></span><br><span class="line">rename <span class="string">"C:\Program Files\Python37\python.exe"</span> python.exe.disabled</span><br><span class="line">rename <span class="string">"C:\Program Files\Python37\Scripts\pip.exe"</span> pip.exe.disabled</span><br><span class="line">python3.bat</span><br><span class="line"></span><br><span class="line">@echo off</span><br><span class="line"></span><br><span class="line">rename <span class="string">"C:\Program Files\Python37\python.exe.disabled"</span> python.exe</span><br><span class="line">rename <span class="string">"C:\Program Files\Python37\Scripts\pip.exe.disabled"</span> pip.exe</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//learnku.com/articles/31141</span></span><br></pre></td></tr></table></figure>
<h3 id="剪刀、石头、布"><a href="#剪刀、石头、布" class="headerlink" title="剪刀、石头、布"></a>剪刀、石头、布</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">#剪刀、石头、布</span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line">guess_list = [<span class="string">"石头"</span>, <span class="string">"剪刀"</span>, <span class="string">"布"</span>]</span><br><span class="line">win_combination = [[<span class="string">"布"</span>, <span class="string">"石头"</span>], [<span class="string">"石头"</span>, <span class="string">"剪刀"</span>], [<span class="string">"剪刀"</span>, <span class="string">"布"</span>]]</span><br><span class="line"><span class="keyword">while</span> True:</span><br><span class="line">    people = input(<span class="string">'请输入：石头,剪刀,布\n'</span>).strip()</span><br><span class="line">    computer = random.choice(guess_list)</span><br><span class="line">    print(<span class="string">'电脑出拳：'</span>+computer)</span><br><span class="line">    <span class="keyword">if</span> people not <span class="keyword">in</span> guess_list:</span><br><span class="line">        print(<span class="string">'咦~~弄啥类你！~~~'</span>)</span><br><span class="line">        <span class="keyword">continue</span></span><br><span class="line">    <span class="keyword">if</span> computer == people:</span><br><span class="line">        print (<span class="string">'平手，再玩一次！'</span>)</span><br><span class="line">    elif [computer, people] <span class="keyword">in</span> win_combination:</span><br><span class="line">        print (<span class="string">'电脑获胜！继续吧~~~'</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        print (<span class="string">'你获胜！'</span>)</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line">    print(<span class="string">'---------------------------------'</span>)</span><br><span class="line"></span><br><span class="line">print(<span class="string">'Press any key to exit'</span>);</span><br><span class="line">input();                                        #防止控制台输出秒退</span><br></pre></td></tr></table></figure>
<h3 id="md5和sha1加密"><a href="#md5和sha1加密" class="headerlink" title="md5和sha1加密"></a>md5和sha1加密</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">import</span> hashlib</span><br><span class="line"> </span><br><span class="line">data =  <span class="string">'This a md5 test!'</span></span><br><span class="line">hash_md5 = hashlib.md5(data)</span><br><span class="line"></span><br><span class="line">hash_md5.hexdigest()</span><br><span class="line">MD5不仅仅是上面这个例子这样用来处理字符串，还有更广泛的用途：</span><br><span class="line">加密网站注册用户的密码。 （但去年的各大网站密码泄漏事件确实让人蛋疼……）</span><br><span class="line">网站用户上传图片 / 文件后，计算出MD5值作为文件名。（MD5可以保证唯一性）</span><br><span class="line">key-value数据库中使用MD5值作为key。</span><br><span class="line">比较两个文件是否相同。（大家在下载一些资源的时候，就会发现网站提供了MD5值，就是用来检测文件是否被篡改）</span><br><span class="line">用MD5来检测两个文件是否相同，但想想，如果是两个很大的文件，担心内存不够用，这时怎么办？</span><br><span class="line">这就要使用 update 方法了。代码如下：</span><br><span class="line"><span class="keyword">import</span> hashlib</span><br><span class="line"></span><br><span class="line">def get_file_md5(f):</span><br><span class="line">    m = hashlib.md5()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> True:</span><br><span class="line">        data = f.read(<span class="number">10240</span>)</span><br><span class="line">        <span class="keyword">if</span> not data:</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line">        m.update(data)</span><br><span class="line">    <span class="keyword">return</span> m.hexdigest()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> open(YOUR_FILE, <span class="string">'r'</span>) <span class="keyword">as</span> f:</span><br><span class="line">    file_md5 = get_file_md5(f)</span><br><span class="line">(windows 用户 要使用 <span class="string">'rb'</span>方式打开文件)</span><br><span class="line">大家可以用下面这段代码验证一下：</span><br><span class="line"><span class="keyword">import</span> hashlib</span><br><span class="line"></span><br><span class="line">x = hashlib.md5()</span><br><span class="line">x.update(<span class="string">'hello, '</span>)</span><br><span class="line">x.update(<span class="string">'python'</span>)</span><br><span class="line">x.hexdigest()</span><br><span class="line"></span><br><span class="line">hashlib.md5(<span class="string">'hello, python'</span>).hexdigest()</span><br><span class="line">这两次的输出是一样的。</span><br><span class="line">SHA1 也是一样的用法。https:<span class="comment">//p0sec.net/index.php/archives/33/</span></span><br></pre></td></tr></table></figure>
<h3 id="concat-组合-dataframe"><a href="#concat-组合-dataframe" class="headerlink" title="concat 组合 dataframe"></a>concat 组合 dataframe</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line">india_weather = pd.DataFrame(&#123;</span><br><span class="line">    <span class="string">'city'</span>: [<span class="string">'mumbai'</span>, <span class="string">'delhi'</span>, <span class="string">'banglore'</span>],</span><br><span class="line">    <span class="string">'temperature'</span>: [<span class="number">32</span>, <span class="number">34</span>, <span class="number">30</span>],</span><br><span class="line">    <span class="string">'humidity'</span>: [<span class="number">80</span>, <span class="number">60</span>, <span class="number">72</span>]</span><br><span class="line">&#125;)</span><br><span class="line">us_weather = pd.DataFrame(&#123;</span><br><span class="line">    <span class="string">'city'</span>: [<span class="string">'newyork'</span>, <span class="string">'chicago'</span>, <span class="string">'orlando'</span>],</span><br><span class="line">    <span class="string">'temperature'</span>: [<span class="number">21</span>, <span class="number">24</span>, <span class="number">32</span>],</span><br><span class="line">    <span class="string">'humidity'</span>: [<span class="number">68</span>, <span class="number">65</span>, <span class="number">70</span>]</span><br><span class="line">&#125;)</span><br><span class="line">df = pd.concat([india_weather, us_weather])</span><br><span class="line">df = pd.concat([india_weather, us_weather], ignore_index=True)</span><br><span class="line"></span><br><span class="line">df = pd.concat([india_weather, us_weather], keys=[<span class="string">'india'</span>, <span class="string">'us'</span>])</span><br><span class="line"></span><br><span class="line">df.loc[<span class="string">'india'</span>]</span><br><span class="line">df = pd.concat([temperature_df, windspeed_df], axis=<span class="number">1</span>)</span><br><span class="line">https:<span class="comment">//learnku.com/articles/26025</span></span><br></pre></td></tr></table></figure>
<h3 id="数学问题"><a href="#数学问题" class="headerlink" title="数学问题"></a>数学问题</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">数学问题：假如一个星球有 <span class="number">100</span> 人，每年人数翻一倍。那么，多少年之后人数才有 <span class="number">100</span> 万人</span><br><span class="line">&gt;&gt;&gt; p=<span class="number">100</span></span><br><span class="line">&gt;&gt;&gt; y=<span class="number">0</span></span><br><span class="line">&gt;&gt;&gt; <span class="keyword">while</span> p&lt;<span class="number">1000000</span>:</span><br><span class="line">...       p*=<span class="number">2</span></span><br><span class="line">...       y+=<span class="number">1</span></span><br><span class="line">...</span><br><span class="line">&gt;&gt;&gt; y</span><br><span class="line"><span class="number">14</span></span><br></pre></td></tr></table></figure>
<h3 id="Python语法速查"><a href="#Python语法速查" class="headerlink" title="Python语法速查"></a>Python语法速查</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">http:<span class="comment">//www.ikeguang.com/2019/03/17/python-sytnax/</span></span><br><span class="line">a = [<span class="number">1</span>, <span class="number">2</span>]</span><br><span class="line">b = a</span><br><span class="line">print(id(a) - id(b))  # 地址差为 0，表示实质是同址的</span><br><span class="line"><span class="number">0</span></span><br><span class="line">b.append(<span class="number">3</span>)</span><br><span class="line">print(a)  # 只改动了 b，但 a 也跟着变动了</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line">a is b</span><br><span class="line">True</span><br><span class="line">使用切片来重新分配空间：</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">a is a[:]</span><br><span class="line">False</span><br><span class="line">运算两数中只要有一个浮点数，结果就是浮点数；</span><br><span class="line">整数相除，即使能除尽，结果也是浮点数；</span><br><span class="line">Python 内部的机制解决了整数溢出的问题，不用担心。</span><br><span class="line">序列主要包括字符串（str）、列表（list）与元祖（tuple）三类。</span><br><span class="line">&gt;&gt;&gt; <span class="string">'ab'</span>.index(<span class="string">'b'</span>)</span><br><span class="line"><span class="number">1</span></span><br><span class="line">&gt;&gt;&gt; <span class="string">'b'</span> <span class="keyword">in</span> <span class="string">'ab'</span></span><br><span class="line">True</span><br><span class="line">&gt;&gt;&gt; max([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>])</span><br><span class="line"><span class="number">3</span></span><br><span class="line">s = " I love Python"  # 首位是空格</span><br><span class="line">lst = s.split(<span class="string">' '</span>)</span><br><span class="line">lst1 = <span class="string">'-'</span>.join(lst)</span><br><span class="line">strip() 去掉字符串首尾两端的空格。方法 lstrip()/rstrip() 则只切除首端/尾端的空格。</span><br><span class="line"><span class="string">'I like &#123;&#125; and &#123;&#125;'</span>.format(<span class="string">'Python'</span>, <span class="string">'you'</span>)</span><br><span class="line"><span class="string">'I like Python and you'</span></span><br><span class="line">'&#123;0&#125; + &#123;2&#125; = &#123;1&#125;'.format (10, 20, 'Python ')  # 按顺序引用</span><br><span class="line"><span class="string">'10 + Python  = 20'</span></span><br><span class="line">'&#123;0&#125; * &#123;1&#125; = &#123;0&#125;'.format (10, 'Python ')  # 编号反复引用</span><br><span class="line"><span class="string">'10 * Python  = 10'</span></span><br></pre></td></tr></table></figure>
<h3 id="bing搜索"><a href="#bing搜索" class="headerlink" title="bing搜索"></a>bing搜索</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests, re, time, webbrowser, codecs</span><br><span class="line">print(<span class="string">'==========搜索引擎=========='</span>)</span><br><span class="line">time.sleep(<span class="number">0.7</span>)</span><br><span class="line">headers = &#123;<span class="string">'Accept'</span>:<span class="string">'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'</span>, </span><br><span class="line"> <span class="string">'Accept-Encoding'</span>:<span class="string">'gzip, deflate, sdch'</span>, </span><br><span class="line"> <span class="string">'Accept-Language'</span>:<span class="string">'zh-CN,zh;q=0.8'</span>, </span><br><span class="line"> <span class="string">'Cache-Control'</span>:<span class="string">'max-age=0'</span>, </span><br><span class="line"> <span class="string">'Connection'</span>:<span class="string">'keep-alive'</span>, </span><br><span class="line"> <span class="string">'Cookie'</span>:<span class="string">'SRCHD=AF=NOFORM; SRCHUID=V=2&amp;GUID=E4CB65F3BD7F4EC7922E3642567A39EC&amp;dmnchg=1; _EDGE_V=1; MUID=24CC781F18B266D70F9C758D199C670F; MUIDB=24CC781F18B266D70F9C758D199C670F; SRCHUSR=DOB=20190707&amp;T=1562487393000; SNRHOP=I=&amp;TS=; _EDGE_S=mkt=zh-cn&amp;SID=2C85ED242A1D66051D4FE0B62B33673B; _SS=SID=2C85ED242A1D66051D4FE0B62B33673B&amp;HV=1562490664; SRCHHPGUSR=CW=1089&amp;CH=1742&amp;DPR=1&amp;UTC=480&amp;WTS=63698084193&amp;PR=3'</span>, </span><br><span class="line"> <span class="string">'DNT'</span>:<span class="string">'1'</span>, </span><br><span class="line"> <span class="string">'Host'</span>:<span class="string">'cn.bing.com'</span>, </span><br><span class="line"> <span class="string">'Upgrade-Insecure-Requests'</span>:<span class="string">'1'</span>, </span><br><span class="line"> <span class="string">'User-Agent'</span>:<span class="string">'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'</span>&#125;</span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        <span class="keyword">while</span> True:</span><br><span class="line">            web_ids = &#123;&#125;</span><br><span class="line">            test = []</span><br><span class="line">            search = input(<span class="string">'输入搜索的内容：'</span>)</span><br><span class="line">            page = [<span class="number">11</span>, <span class="number">21</span>, <span class="number">31</span>, <span class="number">41</span>, <span class="number">51</span>]</span><br><span class="line"></span><br><span class="line">            def <span class="keyword">catch</span>():</span><br><span class="line">                global webs</span><br><span class="line">                id_times = <span class="number">0</span></span><br><span class="line">                <span class="keyword">for</span> t <span class="keyword">in</span> page:</span><br><span class="line">                    url = <span class="string">'https://cn.bing.com/search?q='</span> + search + <span class="string">'&amp;qs=n&amp;sp=-1&amp;pq&amp;sc=0-5&amp;sk=&amp;cvid=275512403280414F9363B7CDC7368CBD&amp;first='</span> + str(t) + <span class="string">'&amp;FORM=PERE'</span></span><br><span class="line">                    text = requests.get(url, headers=headers).text</span><br><span class="line">                    a1 = <span class="string">'&lt;h2&gt;(.*?)&lt;/h2&gt;'</span></span><br><span class="line">                    a2 = <span class="string">'href="(.*?)"'</span></span><br><span class="line">                    a3 = <span class="string">'&gt;(.*?)&lt;/a&gt;'</span></span><br><span class="line">                    title = re.findall(a1, text)</span><br><span class="line">                    <span class="keyword">for</span> j <span class="keyword">in</span> title:</span><br><span class="line">                        id_times = id_times + <span class="number">1</span></span><br><span class="line">                        title = re.findall(a3, j)</span><br><span class="line">                        webs = re.findall(a2, j)</span><br><span class="line">                        web_ids[id_times] = webs</span><br><span class="line">                        print(id_times, title[<span class="number">0</span>])</span><br><span class="line">                        print(<span class="string">'网址：%s'</span> % webs[<span class="number">0</span>])</span><br><span class="line">                        print()</span><br><span class="line">                        <span class="keyword">if</span> len(webs) &gt; <span class="number">0</span>:</span><br><span class="line">                            test.append(<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">                <span class="keyword">if</span> len(test) &gt; <span class="number">0</span>:</span><br><span class="line">                    print(<span class="string">'打开网址（如需打开多个网址，请用英文符号“,”，使用其它键默认不打开任何网址）'</span>)</span><br><span class="line">                    open_web = input(<span class="string">'网址编号：'</span>).split(<span class="string">','</span>)</span><br><span class="line">                    <span class="keyword">if</span> <span class="string">'n'</span> not <span class="keyword">in</span> open_web:</span><br><span class="line">                        <span class="keyword">for</span> aweb <span class="keyword">in</span> open_web:</span><br><span class="line">                            <span class="keyword">if</span> aweb.isdigit():</span><br><span class="line">                                ty = int(aweb)</span><br><span class="line">                                <span class="keyword">if</span> ty <span class="keyword">in</span> web_ids:</span><br><span class="line">                                    web = web_ids[ty]</span><br><span class="line">                                    webbrowser.open(web[<span class="number">0</span>])</span><br><span class="line"></span><br><span class="line">                    <span class="keyword">else</span>:</span><br><span class="line">                        print(<span class="string">'没有符合您的搜索结果！'</span>)</span><br><span class="line">                    print(<span class="string">'=============================='</span>)</span><br><span class="line"></span><br><span class="line">            <span class="keyword">catch</span>()</span><br><span class="line"></span><br><span class="line">    except KeyboardInterrupt:</span><br><span class="line">        exit()</span><br><span class="line"></span><br><span class="line">except requests.exceptions.ConnectionError:</span><br><span class="line">    print(<span class="string">'抱歉，网络出现了一点问题！'</span>)<span class="comment">//https://learnku.com/articles/32422</span></span><br><span class="line">    time.sleep(<span class="number">3</span>)</span><br><span class="line">    exit()</span><br></pre></td></tr></table></figure>
<h3 id="requests-html"><a href="#requests-html" class="headerlink" title="requests-html"></a>requests-html</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br></pre></td><td class="code"><pre><span class="line">pip install requests-html</span><br><span class="line"><span class="keyword">from</span> requests_html <span class="keyword">import</span> HTMLSession</span><br><span class="line">session = HTMLSession()</span><br><span class="line"># GET请求</span><br><span class="line">url = <span class="string">"http://kaoshi.edu.sina.com.cn/college/scorelist?tab=batch&amp;wl=1&amp;local=2&amp;batch=&amp;syear=2013"</span></span><br><span class="line">r = session.get(url)</span><br><span class="line">r.encoding='utf-8'  # 解决中文乱码问题</span><br><span class="line">print(r.text)</span><br><span class="line"># 获取的网页的内容存储到本地</span><br><span class="line"><span class="keyword">with</span> open(<span class="string">'test.html'</span>,<span class="string">'wb'</span>) <span class="keyword">as</span> f:</span><br><span class="line">    f.write(r.content)</span><br><span class="line"></span><br><span class="line"># POST请求</span><br><span class="line">url = <span class="string">'https://shuju.wdzj.com/plat-info-target.html'</span></span><br><span class="line">params = &#123;<span class="string">'wdzjPlatId'</span>: <span class="number">59</span>,<span class="string">'type'</span>: <span class="number">1</span>, <span class="string">'target1'</span>: <span class="number">1</span>, <span class="string">'target2'</span>: <span class="number">0</span>&#125;</span><br><span class="line">r = session.post(url, params=params)</span><br><span class="line">print(r.text)</span><br><span class="line"></span><br><span class="line">###定制请求头</span><br><span class="line">headers = &#123;<span class="string">'user-agent'</span>: <span class="string">'my-app/0.0.1'</span>&#125;</span><br><span class="line">r = session.get(url, headers=headers)</span><br><span class="line">r = session.get(<span class="string">'http://www.w3school.com.cn'</span>)</span><br><span class="line">print(r.html.links)</span><br><span class="line"></span><br><span class="line"># 输出 (太多，中间省略部分)</span><br><span class="line">&#123;<span class="string">'http://www.w3ctech.com/'</span>, <span class="string">'/glossary/index.asp'</span>, <span class="string">'/html5/html5_quiz.asp'</span>, <span class="string">'/php/index.asp'</span>, <span class="string">'/asp/index.asp'</span>, <span class="string">'/php/php_ref_date.asp'</span>, <span class="string">'http://wetest.qq.com/?from=links_w3school'</span>, <span class="string">'/asp/asp_ref.asp'</span>, <span class="string">'/tags/index.asp'</span>, <span class="string">'/xmldom/index.asp'</span>, <span class="string">'/example/csse_examples.asp'</span>, <span class="string">'/w.asp'</span>, <span class="string">'/index.html'</span>, <span class="string">'http://weibo.com/w3schoolcomcn'</span>, <span class="string">'/ws.asp'</span>, <span class="string">'/b.asp'</span>, <span class="string">'/cssref/index.asp'</span>, <span class="string">'/jquerymobile/index.asp'</span>,</span><br><span class="line">...</span><br><span class="line"><span class="string">'/xsl/xsl_languages.asp'</span>,</span><br><span class="line"><span class="string">'/example/html_examples.asp'</span>&#125;</span><br><span class="line"></span><br><span class="line"># 获取绝对地址</span><br><span class="line">t = r.html.absolute_links</span><br><span class="line">print(t)</span><br><span class="line">&#123;<span class="string">'http://www.w3school.com.cn/media/index.asp'</span>,</span><br><span class="line"><span class="string">'http://www.w3school.com.cn/glossary/index.asp'</span>,</span><br><span class="line"><span class="string">'http://www.w3school.com.cn/php/php_ref.asp'</span>,</span><br><span class="line"><span class="string">'http://www.w3school.com.cn/site/index.asp'</span>,</span><br><span class="line">...</span><br><span class="line"><span class="string">'http://www.w3school.com.cn/asp/asp_quiz.asp'</span>&#125;</span><br><span class="line"># 获取3cschoool首页左侧的菜单列表  first=True表示找到的第一个‘HTML教程’</span><br><span class="line">menuList = r.html.find(<span class="string">'#navsecond &gt; ul'</span>, first=True)</span><br><span class="line">print(menuList.text)</span><br><span class="line"></span><br><span class="line"># 输出</span><br><span class="line">HTML</span><br><span class="line">HTML5</span><br><span class="line">XHTML</span><br><span class="line">CSS</span><br><span class="line">CSS3</span><br><span class="line">TCP/IP</span><br><span class="line"></span><br><span class="line"># 找出所有菜单的标题和链接</span><br><span class="line">menuList = r.html.find(<span class="string">'#navsecond &gt; ul'</span>)</span><br><span class="line"><span class="keyword">for</span> menu <span class="keyword">in</span> menuList:</span><br><span class="line">    print(menu.text)  # 获得标题</span><br><span class="line">    print(menu.absolute_links)  # 获得链接</span><br><span class="line"></span><br><span class="line"># 输出</span><br><span class="line">HTML</span><br><span class="line">HTML5</span><br><span class="line">XHTML</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> requests_html <span class="keyword">import</span> HTMLSession</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"></span><br><span class="line">session = HTMLSession()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 背景图片地址</span><br><span class="line">url = <span class="string">"http://www.win4000.com/wallpaper_2285_0_10_1.html"</span></span><br><span class="line">r = session.get(url)</span><br><span class="line"></span><br><span class="line"># 新建bg文件夹</span><br><span class="line"><span class="keyword">if</span> not os.path.exists(<span class="string">'bg'</span>):</span><br><span class="line">    os.mkdir(<span class="string">'bg'</span>)</span><br><span class="line"></span><br><span class="line"># 保存图片到bg/目录</span><br><span class="line">def save_image(url, title):</span><br><span class="line">    img_response = requests.get(url)</span><br><span class="line">    <span class="keyword">with</span> open(<span class="string">'./bg/'</span>+title+<span class="string">'.jpg'</span>, <span class="string">'wb'</span>) <span class="keyword">as</span> file:</span><br><span class="line">        file.write(img_response.content)</span><br><span class="line"></span><br><span class="line"># 查找页面中图片列表，找到链接，</span><br><span class="line"># 点击链接，访问查看大图，并获取大图地址pic-large</span><br><span class="line">items_img = r.html.find(<span class="string">'ul.clearfix &gt; li &gt; a'</span>)</span><br><span class="line"><span class="keyword">for</span> img <span class="keyword">in</span> items_img:</span><br><span class="line">    img_url = img.attrs[<span class="string">'href'</span>]</span><br><span class="line">    <span class="keyword">if</span> <span class="string">"/wallpaper_detail"</span> <span class="keyword">in</span> img_url:</span><br><span class="line">        r = session.get(img_url)          # 解析图片详情</span><br><span class="line">        item_img = r.html.find(<span class="string">'img.pic-large'</span>, first=True)</span><br><span class="line">        url = item_img.attrs['src']       # 大图图片地址</span><br><span class="line">        title = item_img.attrs['title']   # 图片标题</span><br><span class="line">        print(url+title)</span><br><span class="line">        save_image(url, title)</span><br><span class="line">        </span><br><span class="line">http:<span class="comment">//www.golang365.com/#/blog/17</span></span><br></pre></td></tr></table></figure>
<h3 id="输出今天日期"><a href="#输出今天日期" class="headerlink" title="输出今天日期"></a>输出今天日期</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"># 获取今天年月日</span><br><span class="line">nowdate = time.localtime(time.time())  # 获得当前时间戳</span><br><span class="line">today = time.strftime('%Y-%m-%d %H:%M:%S', nowdate)  # 转换成指定格式</span><br><span class="line">print(today)</span><br></pre></td></tr></table></figure>
<h3 id="保存json文件"><a href="#保存json文件" class="headerlink" title="保存json文件"></a>保存json文件</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> io</span><br><span class="line"></span><br><span class="line">sendData = [</span><br><span class="line">  &#123;</span><br><span class="line">    id: <span class="number">1</span>,</span><br><span class="line">    name: <span class="string">'奥特曼'</span></span><br><span class="line">  &#125;,&#123;</span><br><span class="line">    id: <span class="number">2</span>,</span><br><span class="line">    name: <span class="string">'小怪兽'</span></span><br><span class="line">  &#125;</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> io.open(<span class="string">'data.json'</span>, <span class="string">'w'</span>, encoding=<span class="string">"utf-8"</span>) <span class="keyword">as</span> file:</span><br><span class="line">    json.dump(sendData, file, ensure_ascii=False, sort_keys=True, indent=<span class="number">2</span>)</span><br><span class="line">print('保存成功')#http://www.golang365.com/#/blog/18</span><br></pre></td></tr></table></figure>
<h3 id="openCV"><a href="#openCV" class="headerlink" title="openCV"></a>openCV</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">pip install --upgrade setuptools</span><br><span class="line"></span><br><span class="line">pip install numpy Matplotlib</span><br><span class="line"></span><br><span class="line">pip install opencv-python</span><br><span class="line">如果多次下载失败，可以从 http:<span class="comment">//www.lfd.uci.edu/~gohlke/pythonlibs/ 直接下载whl包安装，安装whl包依然使用pip</span></span><br><span class="line"></span><br><span class="line">#导入cv模块</span><br><span class="line"><span class="keyword">import</span> cv2 <span class="keyword">as</span> cv</span><br><span class="line">#读取图像，支持 bmp、jpg、png、tiff 等常用格式</span><br><span class="line">img = cv.imread(r<span class="string">"E:\python\test.jpg"</span>)</span><br><span class="line">#创建窗口并显示图像</span><br><span class="line">cv.namedWindow(<span class="string">"Image"</span>)</span><br><span class="line">cv.imshow(<span class="string">"Image"</span>,img)</span><br><span class="line">cv.waitKey(<span class="number">0</span>)</span><br><span class="line">#释放窗口</span><br><span class="line">cv2.destroyAllWindows() </span><br><span class="line">http:<span class="comment">//www.golang365.com/#/blog/19</span></span><br></pre></td></tr></table></figure>
<h3 id="图片转pdf"><a href="#图片转pdf" class="headerlink" title="图片转pdf"></a>图片转pdf</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line">pip install reportlab</span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line"><span class="keyword">from</span> reportlab.pdfgen <span class="keyword">import</span> canvas</span><br><span class="line"></span><br><span class="line"># 生成多页pdf 生成一个3页的pdf文件</span><br><span class="line">def texttopdf():</span><br><span class="line">    c = canvas.Canvas(<span class="string">'text.pdf'</span>)</span><br><span class="line">    c.drawString(<span class="number">100</span>, <span class="number">100</span>, <span class="string">"Some text in first page."</span>)</span><br><span class="line">    c.showPage()</span><br><span class="line">    c.drawString(<span class="number">100</span>, <span class="number">100</span>, <span class="string">"Some text in second page."</span>)</span><br><span class="line">    c.showPage()</span><br><span class="line">    c.drawString(<span class="number">100</span>, <span class="number">100</span>, <span class="string">"Some text in third page"</span>)</span><br><span class="line">    c.showPage()</span><br><span class="line">    c.save()</span><br><span class="line"></span><br><span class="line">texttopdf()</span><br><span class="line">print(<span class="string">'转换成功！'</span>)</span><br><span class="line"></span><br><span class="line"># 单张图片转pdf，图片不失真。比较清晰</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line"><span class="keyword">from</span> reportlab.lib.pagesizes <span class="keyword">import</span> portrait</span><br><span class="line"><span class="keyword">from</span> reportlab.pdfgen <span class="keyword">import</span> canvas</span><br><span class="line"><span class="keyword">from</span> PIL <span class="keyword">import</span> Image</span><br><span class="line"></span><br><span class="line"># 如果有输指定文件则转换参数里的图片，否则转换test.jpg文件</span><br><span class="line"><span class="keyword">if</span> len(sys.argv) &gt; <span class="number">1</span>:</span><br><span class="line">    img = sys.argv[<span class="number">1</span>]</span><br><span class="line">    filename = img.split(<span class="string">'.'</span>)[<span class="number">0</span>]</span><br><span class="line">    f_jpg = filename+<span class="string">'.jpg'</span></span><br><span class="line">    f_pdf = filename+<span class="string">'.pdf'</span></span><br><span class="line">    print(f_jpg)</span><br><span class="line"></span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    img = <span class="string">'wechat.png'</span></span><br><span class="line">    f_pdf = <span class="string">'test.pdf'</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">def imgtopdf():</span><br><span class="line">    (maxw, maxh) = Image.open(img).size</span><br><span class="line">    c = canvas.Canvas(f_pdf, pagesize=(maxw, maxh))</span><br><span class="line">    c.drawImage(img, <span class="number">0</span>, <span class="number">0</span>, maxw, maxh)</span><br><span class="line">    c.showPage()</span><br><span class="line">    c.save()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">imgtopdf()</span><br><span class="line">print(<span class="string">'转换成功！'</span>)</span><br><span class="line">https:<span class="comment">//github.com/sweida/python-study/tree/master/imgToPdf</span></span><br><span class="line">http:<span class="comment">//www.golang365.com/#/blog/22</span></span><br></pre></td></tr></table></figure>
<h3 id="模拟登录"><a href="#模拟登录" class="headerlink" title="模拟登录"></a>模拟登录</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line">session = requests.session()</span><br><span class="line"></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">'User-Agent'</span>: <span class="string">'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">def login():</span><br><span class="line">    url = <span class="string">'http://119.29.27.100/apis/login'</span></span><br><span class="line">    data = &#123;</span><br><span class="line">        <span class="string">'username'</span>: XXX,</span><br><span class="line">        <span class="string">'password'</span>: *******</span><br><span class="line">    &#125;</span><br><span class="line">    response = session.post(url, data=data, headers=headers)</span><br><span class="line"></span><br><span class="line">    responseData = response.json()</span><br><span class="line">    <span class="keyword">if</span> responseData[<span class="string">'status'</span>]==<span class="number">1</span>:</span><br><span class="line">        print(<span class="string">'登录成功'</span>)</span><br><span class="line">        comment()</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        print(<span class="string">'登录失败'</span>, <span class="string">'失败原因：'</span>, responseData[<span class="string">'msg'</span>])</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">def comment():</span><br><span class="line">    url = <span class="string">'http://119.29.27.100/apis/message/add'</span></span><br><span class="line">    data = &#123;</span><br><span class="line">        <span class="string">'content'</span>: <span class="string">'这条应该是有登录的'</span>,</span><br><span class="line">        <span class="string">'ykname'</span>: <span class="string">''</span></span><br><span class="line">    &#125;</span><br><span class="line">    response = session.post(url, data=data, headers=headers)</span><br><span class="line"></span><br><span class="line">    responseData = response.json()</span><br><span class="line">    <span class="keyword">if</span> responseData[<span class="string">'status'</span>] == <span class="number">1</span>:</span><br><span class="line">        print(<span class="string">'留言成功'</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        print(<span class="string">'留言失败'</span>, <span class="string">'失败原因：'</span>, responseData[<span class="string">'msg'</span>])</span><br><span class="line"></span><br><span class="line">login()</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">'User-Agent'</span>: <span class="string">'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"># 利用浏览器登录后得到的cookie，</span><br><span class="line">cookie_str = r<span class="string">'_myFavMv=%5B%5D; td_cookie=3034830472; laravel_session=eyJpdiI6Ik4wSjFSUU1wcFo1SndYRFliNWZZeXc9PSIsInZhbHVlIjoiMHJWZzM1WmpGRXp6NWVLYk9OaUdHOVVzcWRNK25lQ21lMFhIcmk4eUxKcEFMSnhwSDBMbTFyM3duUllqT3IycGRIc3V2TGhzWEdWaytWRkpzT3hNelE9PSIsIm1hYyI6ImNiMjRhMGFiYTIxYWJhMjUwZDJlNGI2ODgzY2ZiYzY4ZGY4NzI0MDQ4OGZkN2RiNGIwZGM2M2I2YmExYmY3OGIifQ%3D%3D'</span></span><br><span class="line"></span><br><span class="line">#把cookie字符串处理成字典，以便接下来使用</span><br><span class="line">cookies = &#123;&#125;</span><br><span class="line"><span class="keyword">for</span> line <span class="keyword">in</span> cookie_str.split(<span class="string">';'</span>):</span><br><span class="line">    key, value = line.split(<span class="string">'='</span>, <span class="number">1</span>)</span><br><span class="line">    cookies[key] = value</span><br><span class="line"></span><br><span class="line">def comment():</span><br><span class="line">    url = <span class="string">'http://119.29.27.100/apis/message/add'</span></span><br><span class="line">    data = &#123;</span><br><span class="line">        <span class="string">'content'</span>: <span class="string">'再试一条cookie请求'</span>,</span><br><span class="line">        <span class="string">'ykname'</span>: <span class="string">''</span></span><br><span class="line">    &#125;</span><br><span class="line">    response = requests.post(url, data=data, headers=headers, cookies=cookies)</span><br><span class="line"></span><br><span class="line">    responseData = response.json()</span><br><span class="line">    # print(responseData)</span><br><span class="line">    <span class="keyword">if</span> responseData[<span class="string">'status'</span>] == <span class="number">1</span>:</span><br><span class="line">        print(<span class="string">'留言成功'</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        print(<span class="string">'留言失败'</span>, <span class="string">'失败原因：'</span>, responseData[<span class="string">'msg'</span>])</span><br><span class="line"></span><br><span class="line">comment()</span><br><span class="line">http:<span class="comment">//www.golang365.com/#/blog/54</span></span><br></pre></td></tr></table></figure>
<h3 id="猫眼票房"><a href="#猫眼票房" class="headerlink" title="猫眼票房"></a>猫眼票房</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> re</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> datetime</span><br><span class="line"><span class="keyword">import</span> base64</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> requests.exceptions <span class="keyword">import</span> RequestException</span><br><span class="line"><span class="keyword">from</span> pyquery <span class="keyword">import</span> PyQuery <span class="keyword">as</span> pq</span><br><span class="line"><span class="keyword">from</span> fontTools.ttLib <span class="keyword">import</span> TTFont</span><br><span class="line"></span><br><span class="line">font = TTFont(<span class="string">'font1.woff'</span>)</span><br><span class="line">uni_list = font.getGlyphOrder()[<span class="number">2</span>:]</span><br><span class="line">first_match = &#123;</span><br><span class="line">    <span class="string">'uniE893'</span>: <span class="string">'0'</span>,</span><br><span class="line">    <span class="string">'uniF690'</span>: <span class="string">'1'</span>,</span><br><span class="line">    <span class="string">'uniF55C'</span>: <span class="string">'2'</span>,</span><br><span class="line">    <span class="string">'uniF28F'</span>: <span class="string">'3'</span>,</span><br><span class="line">    <span class="string">'uniF4B1'</span>: <span class="string">'4'</span>,</span><br><span class="line">    <span class="string">'uniE623'</span>: <span class="string">'5'</span>,</span><br><span class="line">    <span class="string">'uniF294'</span>: <span class="string">'6'</span>,</span><br><span class="line">    <span class="string">'uniEEC4'</span>: <span class="string">'7'</span>,</span><br><span class="line">    <span class="string">'uniE577'</span>: <span class="string">'8'</span>,</span><br><span class="line">    <span class="string">'uniE77B'</span>: <span class="string">'9'</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">def get_one_page(date):</span><br><span class="line">    headers = &#123;</span><br><span class="line">        <span class="string">'User-Agent'</span>: os.getenv(<span class="string">'User_Agent'</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    url = <span class="string">'https://piaofang.maoyan.com/?ver=normal&amp;date='</span> + date</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        response = requests.get(url, headers=headers)</span><br><span class="line">        <span class="keyword">if</span> response.status_code == <span class="number">200</span>:</span><br><span class="line">            <span class="keyword">return</span> response.text</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="keyword">return</span> None</span><br><span class="line">    except RequestException <span class="keyword">as</span> e:</span><br><span class="line">        print(<span class="string">"Requests &#123;&#125;, Error &#123;&#125;."</span>.format(date, e.args))</span><br><span class="line">        <span class="keyword">return</span> None</span><br><span class="line"></span><br><span class="line">def parse_font(html):</span><br><span class="line">    fonts = re.findall(r<span class="string">'base64,(.*?)\)'</span>, html, re.S)[<span class="number">0</span>]</span><br><span class="line">    # fonts = re.search(r'base64,(.*?)\)', html, re.S)</span><br><span class="line">    fonts = base64.b64decode(fonts)</span><br><span class="line">    <span class="keyword">with</span> open(<span class="string">'tmp.woff'</span>, <span class="string">'wb'</span>) <span class="keyword">as</span> fp:</span><br><span class="line">        fp.write(fonts)</span><br><span class="line">    font1 = TTFont(<span class="string">'tmp.woff'</span>)</span><br><span class="line">    # obj_list1 = font1.getGlyphNames()[1:-1]</span><br><span class="line">    uni_list1 = font1.getGlyphOrder()[<span class="number">2</span>:]</span><br><span class="line">    tmp_match = &#123;&#125;</span><br><span class="line">    <span class="keyword">for</span> uni1 <span class="keyword">in</span> uni_list1:</span><br><span class="line">        obj1 = font1['glyf'][uni1]  #获取编码 uni1 在 tmp.ttf 中对应的对象</span><br><span class="line">        <span class="keyword">for</span> uni <span class="keyword">in</span> uni_list:</span><br><span class="line">            obj = font[<span class="string">'glyf'</span>][uni]</span><br><span class="line">            <span class="keyword">if</span> obj==obj1:</span><br><span class="line">                tmp_match[uni1] = first_match[uni]</span><br><span class="line">    <span class="keyword">return</span> tmp_match</span><br><span class="line"></span><br><span class="line">def rebuild_number(number, tmp_match):</span><br><span class="line">    <span class="string">''</span><span class="string">'还需要对数字进行改写'</span><span class="string">''</span></span><br><span class="line">    result = <span class="string">''</span></span><br><span class="line">    <span class="keyword">for</span> num <span class="keyword">in</span> number:</span><br><span class="line">        s = str(hex(ord(num)))</span><br><span class="line">        s = s.upper().replace(<span class="string">'0X'</span>, <span class="string">'uni'</span>)</span><br><span class="line">        <span class="keyword">if</span> s <span class="keyword">in</span> tmp_match.keys():</span><br><span class="line">            result += tmp_match[s]</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            result += num</span><br><span class="line">    <span class="keyword">return</span> result</span><br><span class="line"></span><br><span class="line">def parse_one_page(html):</span><br><span class="line">    tmp_match = parse_font(html)</span><br><span class="line">    doc = pq(html)</span><br><span class="line">    today = doc(<span class="string">'.today'</span>).text()[:<span class="number">10</span>]</span><br><span class="line">    movies = doc(<span class="string">'#ticket_tbody ul'</span>).items()</span><br><span class="line">    <span class="keyword">for</span> movie <span class="keyword">in</span> movies:</span><br><span class="line">        result = &#123;&#125;</span><br><span class="line">        result[<span class="string">'date'</span>] =  today</span><br><span class="line">        result[<span class="string">'movieName'</span>] = movie.find(<span class="string">'.c1 b'</span>).text()</span><br><span class="line">        result[<span class="string">'releaseInfo'</span>] = movie.find(<span class="string">'.c1 em'</span>).text().split()[<span class="number">0</span>]</span><br><span class="line">        result[<span class="string">'sumBoxInfo'</span>] = rebuild_number(movie.find(<span class="string">'.c1 em i'</span>).text(), tmp_match)</span><br><span class="line">        result[<span class="string">'boxInfo'</span>] =  rebuild_number(movie.find(<span class="string">'.c2'</span>).text(), tmp_match)</span><br><span class="line">        result[<span class="string">'boxRate'</span>] = rebuild_number(movie.find(<span class="string">'.c3'</span>).text(), tmp_match)</span><br><span class="line">        result[<span class="string">'showRate'</span>] = rebuild_number(movie.find(<span class="string">'.c4'</span>).text(), tmp_match)</span><br><span class="line">        result[<span class="string">'avgSeatView'</span>] = rebuild_number(movie.find(<span class="string">'.c5'</span>).text(), tmp_match)</span><br><span class="line">        <span class="keyword">yield</span> result</span><br><span class="line"></span><br><span class="line">def main():</span><br><span class="line">    start_date = datetime.date.today()</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">0</span>, <span class="number">31</span>):</span><br><span class="line">        date = start_date - datetime.timedelta(days=i)</span><br><span class="line">        html = get_one_page(date.isoformat())</span><br><span class="line">        <span class="keyword">for</span> result <span class="keyword">in</span> parse_one_page(html):</span><br><span class="line">            print(result)</span><br><span class="line">        time.sleep(<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    main()</span><br><span class="line">    https:<span class="comment">//learnku.com/articles/32534#reply104205</span></span><br></pre></td></tr></table></figure>
<h3 id="锟斤拷"><a href="#锟斤拷" class="headerlink" title="锟斤拷"></a>锟斤拷</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; s = (u<span class="string">'\uFFFD'</span>.encode(<span class="string">'utf8'</span>)*<span class="number">2</span>)</span><br><span class="line">&gt;&gt;&gt; print(s.decode(<span class="string">'gbk'</span>))</span><br><span class="line">锟斤拷</span><br><span class="line">当unicode遇到解释失败的字时，会尝试用 「U+FFFD」 来代替，「U+FFFD」乃是 unicode 的一个占位符， 显示为 �</span><br><span class="line">http:<span class="comment">//cuihuan.net/2019/05/12/%E5%AD%97%E7%AC%A6%E7%BC%96%E7%A0%81%E9%82%A3%E4%BA%9B%E4%BA%8B%E5%84%BF/</span></span><br></pre></td></tr></table></figure>
<p>###<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">pip intall nonude</span><br><span class="line"><span class="keyword">import</span> nude</span><br><span class="line">print(nude.is_nude(<span class="string">"godfather.jpg"</span>))</span><br><span class="line">print(nude.is_nude(<span class="string">"leisheng.jpg"</span>))</span><br><span class="line">print(nude.is_nude(<span class="string">"qiaoba.png"</span>))</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> glob</span><br><span class="line"><span class="keyword">import</span> itertools</span><br><span class="line"><span class="keyword">from</span> nude <span class="keyword">import</span> Nude</span><br><span class="line"></span><br><span class="line">images_format = ['jpg', 'png', 'gif']  # 图片格式</span><br><span class="line">images_jpg = glob.glob("E:/Images/OOXX/*.jpg")  # 返回匹配指定模式的文件名</span><br><span class="line">images_png = glob.glob(<span class="string">"E:/Images/OOXX/*.png"</span>)</span><br><span class="line">images_gif = glob.glob(<span class="string">"E:/Images/OOXX/*.gif"</span>)</span><br><span class="line"></span><br><span class="line">images_list = itertools.chain(images_jpg, images_png, images_gif)</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> images_list:</span><br><span class="line">    print(i)  # 输出照片的路径</span><br><span class="line">    n = Nude(i)  # 对图片进行识别</span><br><span class="line">    n.parse()</span><br><span class="line">    print(n.result)  # 输出结果</span><br><span class="line">    print(n.message)  # 输出判断信息</span><br><span class="line">    print(n.inspect())  # 输出更加详细的判断信息</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">原文: https:<span class="comment">//lbjheiheihei.xyz/2018/05/14/Use-Python-Identifying-Porngraphic-Images.html</span></span><br></pre></td></tr></table></figure></p>
<h3 id="安装scrapy失败"><a href="#安装scrapy失败" class="headerlink" title="安装scrapy失败"></a>安装scrapy失败</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">依次安装lxml、pyOpenSSL、Twisted、pywin32 这些基本库都要安装好。</span><br><span class="line">    </span><br><span class="line">   pip install lxml</span><br><span class="line">  </span><br><span class="line">   </span><br><span class="line">   </span><br><span class="line">   如果不行，则去下面的网站下载。</span><br><span class="line">   </span><br><span class="line">   https:<span class="comment">//pypi.org/project/lxml/#files</span></span><br><span class="line">    https:<span class="comment">//www.lfd.uci.edu/~gohlke/pythonlibs/#lxml</span></span><br><span class="line">   比如我这台电脑是 Python3<span class="number">.6</span>，<span class="number">32</span>位的就下载 lxml<span class="number">-4.2</span><span class="number">.1</span>-cp36-cp36m-win32.whl 进入 cmd，然后 cd 到文件的路径下，接着就是</span><br><span class="line">   </span><br><span class="line">   pip install lxml<span class="number">-4.2</span><span class="number">.1</span>-cp36-cp36m-win32.whl</span><br><span class="line">    </span><br><span class="line">   命令后面那一部分要和文件名保持一致，也就是 pip install 文件名.whl回车，等一会就安装好了</span><br><span class="line">  pip install lxml</span><br><span class="line">  PythonCopy</span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">  如果不行，则去下面的网站下载。</span><br><span class="line">  </span><br><span class="line">  https:<span class="comment">//pypi.org/project/lxml/#files https://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml</span></span><br><span class="line">  比如我这台电脑是 Python3<span class="number">.6</span>，<span class="number">32</span>位的就下载 lxml<span class="number">-4.2</span><span class="number">.1</span>-cp36-cp36m-win32.whl 进入 cmd，然后 cd 到文件的路径下，接着就是</span><br><span class="line">  </span><br><span class="line">  pip install lxml<span class="number">-4.2</span><span class="number">.1</span>-cp36-cp36m-win32.whl</span><br><span class="line">  PythonCopy</span><br><span class="line">  命令后面那一部分要和文件名保持一致，也就是 pip install 文件名.whl回车，等一会就安装好了</span><br><span class="line">  </span><br><span class="line">   </span><br><span class="line">  原文: https:<span class="comment">//lbjheiheihei.xyz/2018/05/27/Install-Scrapy-In-Window.html</span></span><br></pre></td></tr></table></figure>
<h3 id="ChromeDriver"><a href="#ChromeDriver" class="headerlink" title="ChromeDriver"></a>ChromeDriver</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//sites.google.com/a/chromium.org/chromedriver/downloads http://phantomjs.org/</span></span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line">options = webdriver.ChromeOptions()</span><br><span class="line">options.add_argument("--incognito")  # 隐身模式打开</span><br><span class="line">driver_path = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"  # chromedriver.exe 的路径</span><br><span class="line">browser = webdriver.Chrome(executable_path=driver_path, options=options)</span><br><span class="line">browser.get("https://kejibear.xyz/auth/login")  # 网址</span><br><span class="line">browser.find_element_by_css_selector(".card-inner input[name='Email']").send_keys("@qq.com")  # 账号</span><br><span class="line">browser.find_element_by_css_selector(".card-inner input[name='Password']").send_keys("1")  # 密码</span><br><span class="line">browser.find_element_by_css_selector(<span class="string">".row .col-md-10.col-md-push-1 button.waves-effect"</span>).click()</span><br><span class="line">print(<span class="string">"登录成功~"</span>)</span><br><span class="line">time.sleep(<span class="number">3</span>)</span><br><span class="line">browser.find_element_by_css_selector(<span class="string">".card-action-btn #checkin-btn button.waves-effect"</span>).click()</span><br><span class="line">print(<span class="string">"签到成功~"</span>)</span><br><span class="line">time.sleep(<span class="number">5</span>)</span><br><span class="line">browser.close()</span><br></pre></td></tr></table></figure>
<h3 id="生成彩色动态二维码"><a href="#生成彩色动态二维码" class="headerlink" title="生成彩色动态二维码"></a>生成彩色动态二维码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">pip install myqr</span><br><span class="line"><span class="keyword">from</span> MyQR <span class="keyword">import</span> myqr</span><br><span class="line">version, level, qr_name = myqr.run(</span><br><span class="line">    words='dhb cdfb64%vjk',  # 不支持中文，支持 0~9,a~z, A~Z 以及常见的常用英文标点符号和空格</span><br><span class="line">    version=2,  # 版本，从 1至 40</span><br><span class="line">    level='H',  # 纠错等级，范围是L、M、Q、H，从左到右依次升高</span><br><span class="line">    picture='4e.jpg',  # 文件要放在目录下</span><br><span class="line">    colorized=True,   # True 为彩色，False 为黑白</span><br><span class="line">    contrast=1.0,  # 对比度</span><br><span class="line">    brightness=1.0,  # 亮度</span><br><span class="line">    save_name='1d6.bmp',  # 命名随便都行，格式可以是 jpg,png,bmp,gif</span><br><span class="line">    save_dir="F:\二维码"  # 路径要存在</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">myqr <span class="number">666</span> -p <span class="number">666.</span>png -c</span><br><span class="line">https:<span class="comment">//lbjheiheihei.xyz/2018/04/26/Use-Python-Generate-Colorful-QRcode.html</span></span><br></pre></td></tr></table></figure>
<h3 id="爬取简书用户的动态"><a href="#爬取简书用户的动态" class="headerlink" title="爬取简书用户的动态"></a>爬取简书用户的动态</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> lxml <span class="keyword">import</span> etree</span><br><span class="line"></span><br><span class="line">my_header = <span class="string">"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"</span></span><br><span class="line"></span><br><span class="line">res = requests.get(url=<span class="string">'https://www.jianshu.com/users/5aa8494a18c8/timeline'</span>, headers=&#123;<span class="string">'user-agent'</span>: my_header&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> <span class="string">'大神带我来搬砖'</span> <span class="keyword">in</span> res.text:</span><br><span class="line">    print(<span class="string">'found'</span>)</span><br><span class="line">page = etree.HTML(res.text)</span><br><span class="line">last_li = page.xpath(<span class="string">''</span><span class="string">'//ul[@class="note-list"]/li[last()]'</span><span class="string">''</span>)[<span class="number">0</span>]</span><br><span class="line">max_id = int(last_li.get(<span class="string">'id'</span>).split(<span class="string">'-'</span>)[<span class="number">1</span>]) - <span class="number">1</span></span><br><span class="line"></span><br><span class="line">file = open(<span class="string">"activity.txt"</span>,<span class="string">'w'</span>,encoding=<span class="string">'utf-8'</span>)</span><br><span class="line"></span><br><span class="line">page = <span class="number">2</span></span><br><span class="line"><span class="keyword">while</span> True:</span><br><span class="line">    res = requests.get(url=<span class="string">'https://www.jianshu.com/users/5aa8494a18c8/timeline?max_id=%s&amp;page=%s'</span> %(max_id,page),</span><br><span class="line">        headers=&#123;<span class="string">'user-agent'</span>: my_header, <span class="string">'X-INFINITESCROLL'</span>:<span class="string">'true'</span>&#125;)</span><br><span class="line"></span><br><span class="line">    last_li = etree.HTML(res.text).xpath(<span class="string">''</span><span class="string">'/html/body/li[last()]'</span><span class="string">''</span>)[<span class="number">0</span>]</span><br><span class="line">    max_id = int(last_li.get(<span class="string">'id'</span>).split(<span class="string">'-'</span>)[<span class="number">1</span>]) - <span class="number">1</span></span><br><span class="line">    page = page + <span class="number">1</span></span><br><span class="line">    file.write(res.text)</span><br><span class="line">    file.write(<span class="string">"\n"</span>)</span><br><span class="line">    <span class="keyword">if</span> <span class="string">'加入了简书'</span> <span class="keyword">in</span> res.text:</span><br><span class="line">        print(<span class="string">'end'</span>)</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line">file.close()</span><br><span class="line">#https://www.jianshu.com/p/35a85ee14f7b</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line">options = webdriver.ChromeOptions()</span><br><span class="line">prefs = &#123;<span class="string">"profile.managed_default_content_settings.images"</span>:<span class="number">2</span>&#125;</span><br><span class="line">options.add_experimental_option(<span class="string">"prefs"</span>,prefs)</span><br><span class="line">browser = webdriver.Chrome(chrome_options=options)</span><br><span class="line">browser.set_page_load_timeout(<span class="number">60</span>)</span><br><span class="line"></span><br><span class="line">browser.get(<span class="string">"https://www.jianshu.com/users/5aa8494a18c8/timeline"</span>)</span><br><span class="line">time.sleep(<span class="number">5</span>)</span><br><span class="line"></span><br><span class="line">file = open(<span class="string">"browser.txt"</span>,<span class="string">'w'</span>,encoding=<span class="string">'utf-8'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> True:</span><br><span class="line">    text = browser.find_element_by_xpath(<span class="string">""</span><span class="string">"//*[@id="</span>list-container<span class="string">"]/ul"</span><span class="string">""</span>).text</span><br><span class="line">    file.write(text)</span><br><span class="line">    # remove li elements</span><br><span class="line">    js=<span class="string">''</span><span class="string">'var nodeList=document.querySelectorAll("#list-container &gt; ul &gt; li");for(var i=0;i&lt;nodeList.length-1;i++)&#123;nodeList[i].remove()&#125;'</span><span class="string">''</span></span><br><span class="line">    browser.execute_script(js)</span><br><span class="line">    </span><br><span class="line">    # scroll</span><br><span class="line">    browser.execute_script(<span class="string">"document.documentElement.scrollTop=0"</span>)</span><br><span class="line">    browser.execute_script(<span class="string">"document.documentElement.scrollTop=1600"</span>)</span><br><span class="line">    time.sleep(<span class="number">10</span>)</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span> <span class="string">'加入了简书'</span> <span class="keyword">in</span> text:</span><br><span class="line">        print(<span class="string">"end"</span>)</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line">file.write(text)</span><br><span class="line">file.close()</span><br></pre></td></tr></table></figure>
<h3 id="retrying-重试请求"><a href="#retrying-重试请求" class="headerlink" title="retrying 重试请求"></a>retrying 重试请求</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">pip install retrying</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> retrying <span class="keyword">import</span> retry</span><br><span class="line"></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">"User-Agent"</span>: <span class="string">"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">@retry(stop_max_attempt_number=3) # 表示重试以下代码三次</span><br><span class="line">def _parse_url(url):</span><br><span class="line">    print(<span class="string">"-"</span> * <span class="number">30</span>)</span><br><span class="line">    response = requests.get(url, headers=headers, timeout=<span class="number">3</span>)</span><br><span class="line">    assert response.status_code == <span class="number">200</span></span><br><span class="line">    <span class="keyword">return</span> response.content.decode()</span><br><span class="line"></span><br><span class="line">def parse_url(url):</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        html_str = _parse_url(url)</span><br><span class="line">    except:</span><br><span class="line">        html_str = None</span><br><span class="line">    <span class="keyword">return</span> html_str</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">"__main__"</span>:</span><br><span class="line">    url = <span class="string">'www.baidu.com'</span></span><br><span class="line">    print(parse_url(url))</span><br><span class="line">    </span><br><span class="line">    https:<span class="comment">//learnku.com/articles/33001</span></span><br></pre></td></tr></table></figure>
<h3 id="ipython"><a href="#ipython" class="headerlink" title="ipython"></a>ipython</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">使用 Tab 实现自动补全功能。</span><br><span class="line">使用问号？呈现对象的说明。</span><br><span class="line">提供了一系列以 % 开头的魔术命令为常见任务的执行提供便利。</span><br><span class="line">提供了额外的绘图功能。</span><br><span class="line">可使用操作系统中的命令。</span><br><span class="line">用 %quickref 查看 IPython 的参考手册，也可以使用 %magic 来查看 IPython 中魔术命令的信息，以更好的使用 IPython 工具。</span><br><span class="line">最近两次执行的结果 (Out) 分别保存在 _ 及 __ 变量中。而且，IPython 还采用 _iN, _N 的方式将所有输入及输出的历史记录进行保存～其中 N 为行号</span><br><span class="line"></span><br><span class="line">In [8]: eval(_i4) # eval('1 + 7*9')</span><br><span class="line">Out[<span class="number">8</span>]: <span class="number">64</span> </span><br><span class="line"> IPython 中执行过的命令，可通过 %logstart 将之保存在 Python 文件中</span><br><span class="line"> In [<span class="number">22</span>]: !ls</span><br><span class="line"> IPython.md                      data_science.md                 pandas.ipynb</span><br><span class="line"> Jupyter.ipynb                   README.md</span><br><span class="line"> </span><br><span class="line"> IPython 同样提供了一些代码分析工具，如代码执行时间的分析工具 %time 和 %timeit、基本的性能分析 %prun 和 %run -p 等</span><br><span class="line"> </span><br><span class="line"> %time：整体执行一次，给出执行时间；</span><br><span class="line"> %timeit：多次执行，给出平均时间。</span><br><span class="line"> In [<span class="number">42</span>]: %time <span class="string">'foobar'</span>.startswith(<span class="string">'foo'</span>)</span><br><span class="line"> CPU times: user <span class="number">3</span> µs, <span class="attr">sys</span>: <span class="number">0</span> ns, <span class="attr">total</span>: <span class="number">3</span> µs</span><br><span class="line"> Wall time: <span class="number">5.96</span> µs</span><br><span class="line"> Out[<span class="number">42</span>]: True</span><br><span class="line"> https:<span class="comment">//learnku.com/articles/33122#reply105653</span></span><br></pre></td></tr></table></figure>
<h3 id="列表中出现次数最多的数"><a href="#列表中出现次数最多的数" class="headerlink" title="列表中出现次数最多的数"></a>列表中出现次数最多的数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">test = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">4</span>]</span><br><span class="line">print(max(set(test), key=test.count))</span><br></pre></td></tr></table></figure>
<h3 id="pygame-模块"><a href="#pygame-模块" class="headerlink" title="pygame 模块"></a>pygame 模块</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pygame</span><br><span class="line"><span class="keyword">from</span> pygame <span class="keyword">import</span>*</span><br><span class="line">pygame.init()</span><br><span class="line">size = (600,400) #窗口的大小</span><br><span class="line">screem = pygame.display.set_mode(size) #把窗口的大小放进pygame.display.set_mode()函数里进行创建。</span><br><span class="line">screem.fill((<span class="number">250</span>,<span class="number">250</span>,<span class="number">250</span>))</span><br><span class="line">写好了，但是窗口还是黑色，那是因为窗口还没刷新，用 pygame.display.update () 来进行刷新</span><br><span class="line">https:<span class="comment">//learnku.com/articles/33209#reply105837</span></span><br></pre></td></tr></table></figure>
<h3 id="发送html格式邮件"><a href="#发送html格式邮件" class="headerlink" title="发送html格式邮件"></a>发送html格式邮件</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/usr/bin/python</span></span><br><span class="line"># -*- coding: utf-8 -*-</span><br><span class="line"><span class="keyword">import</span> smtplib, time, os</span><br><span class="line"><span class="keyword">from</span> email.mime.text <span class="keyword">import</span> MIMEText</span><br><span class="line"><span class="keyword">from</span> email.header <span class="keyword">import</span> Header</span><br><span class="line"></span><br><span class="line">def send_mail_html(file):</span><br><span class="line">    sender = 'admin@jinchuang.org' #发件人</span><br><span class="line">    receiver = 'gaojing@jinchuang.org' #收件人</span><br><span class="line">    t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())  #获取当前时间</span><br><span class="line">    subject = '博客磁盘使用报警_' + t  #邮件主题</span><br><span class="line">    smtpserver = 'smtp.exmail.qq.com' #发送服务器地址</span><br><span class="line">    username = 'admin@jinchuang.org' #用户名</span><br><span class="line">    password = 'passwd' #密码</span><br><span class="line"></span><br><span class="line">    f = open(file, <span class="string">'rb'</span>)</span><br><span class="line">    mail_body = f.read()</span><br><span class="line">    f.close()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    msg = MIMEText(mail_body, _subtype=<span class="string">'html'</span>, _charset=<span class="string">'utf-8'</span>)</span><br><span class="line">    msg[<span class="string">'Subject'</span>] = Header(subject, <span class="string">'utf-8'</span>)</span><br><span class="line">    msg[<span class="string">'From'</span>] = sender</span><br><span class="line">    msg[<span class="string">'To'</span>] = receiver</span><br><span class="line"></span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        smtp = smtplib.SMTP()</span><br><span class="line">        smtp.connect(smtpserver)</span><br><span class="line">        smtp.login(username, password)</span><br><span class="line">        smtp.sendmail(sender, receiver, msg.as_string())</span><br><span class="line">    except:</span><br><span class="line">        print(<span class="string">"邮件发送失败！"</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        print(<span class="string">"邮件发送成功！"</span>)</span><br><span class="line">    <span class="keyword">finally</span>:</span><br><span class="line">        smtp.quit()</span><br><span class="line"></span><br><span class="line">file = '/tmp/df.html' #html文件</span><br><span class="line">send_mail_html(file)</span><br><span class="line"></span><br><span class="line">html表格模板</span><br><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line">ip=<span class="string">`ifconfig |grep -v 127 |grep inet|awk '&#123;print $2&#125;'`</span></span><br><span class="line">a=<span class="string">`df -hT|grep -w "/"|awk '&#123;print $1&#125;'`</span></span><br><span class="line">b=<span class="string">`df -hT|grep -w "/"|awk '&#123;print $2&#125;'`</span></span><br><span class="line">c=<span class="string">`df -hT|grep -w "/"|awk '&#123;print $3&#125;'`</span></span><br><span class="line">d=<span class="string">`df -hT|grep -w "/"|awk '&#123;print $4&#125;'`</span></span><br><span class="line">e=<span class="string">`df -hT|grep -w "/"|awk '&#123;print $5&#125;'`</span></span><br><span class="line">f=<span class="string">`df -hT|grep -w "/"|awk '&#123;print $6&#125;'`</span></span><br><span class="line">g=<span class="string">`df -hT|grep -w "/"|awk '&#123;print $7&#125;'`</span></span><br><span class="line"></span><br><span class="line">html=<span class="string">"&lt;html&gt;</span></span><br><span class="line"><span class="string">&lt;meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\"&gt;</span></span><br><span class="line"><span class="string">&lt;head&gt;</span></span><br><span class="line"><span class="string">&lt;style type=\"text/css\"&gt;</span></span><br><span class="line"><span class="string">table&#123;margin-top:5%;width:500px&#125;</span></span><br><span class="line"><span class="string">table.gridtable &#123;</span></span><br><span class="line"><span class="string">    font-family: verdana,arial,sans-serif;</span></span><br><span class="line"><span class="string">    font-size:14px;</span></span><br><span class="line"><span class="string">    color:#333333;</span></span><br><span class="line"><span class="string">    border-width: 1px;</span></span><br><span class="line"><span class="string">    border-color: #666666;</span></span><br><span class="line"><span class="string">    border-collapse: collapse;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">table.gridtable th &#123;</span></span><br><span class="line"><span class="string">    border-width: 1px;</span></span><br><span class="line"><span class="string">    padding: 8px;</span></span><br><span class="line"><span class="string">    background-color: #008eff;</span></span><br><span class="line"><span class="string">    color:#fff;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">table.gridtable td &#123;</span></span><br><span class="line"><span class="string">    border-width: 1px;</span></span><br><span class="line"><span class="string">    padding: 8px;</span></span><br><span class="line"><span class="string">    border-style: solid;</span></span><br><span class="line"><span class="string">    border-color: #afafaf;</span></span><br><span class="line"><span class="string">    background-color: #ffffff;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">table tr:first-child td:first-child, table tr:first-child th:first-child&#123;</span></span><br><span class="line"><span class="string">  border-top-left-radius: 5px;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">table tr:first-child td:last-child, table tr:first-child th:last-child&#123;</span></span><br><span class="line"><span class="string">  border-top-right-radius: 5px;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">&lt;/style&gt;</span></span><br><span class="line"><span class="string">&lt;/head&gt;</span></span><br><span class="line"><span class="string">&lt;body&gt;</span></span><br><span class="line"><span class="string">&lt;table class=\"gridtable\" align=center&gt;</span></span><br><span class="line"><span class="string">&lt;tr&gt;</span></span><br><span class="line"><span class="string">    &lt;th colspan="</span><span class="number">7</span><span class="string">"&gt;告警主机:192.168.11.1&lt;/th&gt;</span></span><br><span class="line"><span class="string">&lt;/tr&gt;</span></span><br><span class="line"><span class="string">&lt;tr&gt;</span></span><br><span class="line"><span class="string">    &lt;td&gt;文件系统&lt;/td&gt;&lt;td&gt;类型&lt;/td&gt;&lt;td&gt;总共&lt;/td&gt;&lt;td&gt;已用&lt;/td&gt;&lt;td&gt;可用&lt;/td&gt;&lt;td&gt;使用率&lt;/td&gt;&lt;td&gt;挂载点&lt;/td&gt;</span></span><br><span class="line"><span class="string">&lt;/tr&gt;</span></span><br><span class="line"><span class="string">&lt;tr&gt;</span></span><br><span class="line"><span class="string">    &lt;td&gt;$a&lt;/td&gt;&lt;td&gt;$b&lt;/td&gt;&lt;td&gt;$c&lt;/td&gt;&lt;td&gt;$d&lt;/td&gt;&lt;td&gt;$e&lt;/td&gt;&lt;td style=\"color:red;font-weight:bold\"&gt;$f&lt;/td&gt;&lt;td&gt;$g&lt;/td&gt;</span></span><br><span class="line"><span class="string">&lt;/tr&gt;</span></span><br><span class="line"><span class="string">&lt;/table&gt;</span></span><br><span class="line"><span class="string">&lt;/body&gt;</span></span><br><span class="line"><span class="string">&lt;/html&gt;"</span></span><br><span class="line">echo -e <span class="string">"$html"</span> &gt;<span class="regexp">/tmp/</span>df.html</span><br><span class="line"></span><br><span class="line">如果需要使用ssl协议，修改<span class="number">2</span>个地方</span><br><span class="line">smtpserver = 'smtp.exmail.qq.com:465' #加上465端口</span><br><span class="line">smtp = smtplib.SMTP_SSL() #加上ssl</span><br><span class="line">https:<span class="comment">//me.jinchuang.org/archives/272.html</span></span><br></pre></td></tr></table></figure>
<h3 id="pdf"><a href="#pdf" class="headerlink" title="pdf"></a>pdf</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pdfkit</span><br><span class="line"></span><br><span class="line">pdfkit.from_file(<span class="string">'jianshu.htm'</span>,<span class="string">'out.pdf'</span>)   </span><br><span class="line">pdfkit.from_string(<span class="string">'HelloWorld'</span>,<span class="string">'out.pdf'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> open(<span class="string">'jianshu.htm'</span>,<span class="string">'r'</span>) <span class="keyword">as</span> f:</span><br><span class="line">	pdfkit.from_file(f,<span class="string">'out.pdf'</span>)   </span><br><span class="line">	pdfkit.from_url([<span class="string">'https://www.jianshu.com/'</span>,<span class="string">'https://www.baidu.com/'</span>],<span class="string">'out.pdf'</span>)</span><br><span class="line">    </span><br><span class="line">    #pdfkit.from_file(['jianshu.htm','jianshu1.htm'],'out.pdf')  </span><br><span class="line">    </span><br><span class="line">     options=&#123;</span><br><span class="line">        'page-size':'A4',#Letter</span><br><span class="line">         <span class="string">'margin-top'</span>:<span class="string">'0.75in'</span>,</span><br><span class="line">         <span class="string">'margin-right'</span>:<span class="string">'0.75in'</span>,</span><br><span class="line">         <span class="string">'margin-bottom'</span>:<span class="string">'0.75in'</span>,</span><br><span class="line">         <span class="string">'margin-left'</span>:<span class="string">'0.75in'</span>,</span><br><span class="line">         <span class="string">'encoding'</span>:<span class="string">"UTF-8"</span>,</span><br><span class="line">         <span class="string">'no-outline'</span>:None</span><br><span class="line">     &#125;    </span><br><span class="line">     pdfkit.from_url(<span class="string">'https://www.jianshu.com/'</span>,<span class="string">'out1.pdf'</span>, options=options)</span><br><span class="line">     </span><br><span class="line">     https:<span class="comment">//juejin.im/post/5ce69794e51d4577523f22ef</span></span><br></pre></td></tr></table></figure>
<h3 id="天善博客内容如何转成PDF文档"><a href="#天善博客内容如何转成PDF文档" class="headerlink" title="天善博客内容如何转成PDF文档"></a>天善博客内容如何转成PDF文档</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">wkhtmltopdf  <span class="string">'http://www.flybi.net/blog/seng/3645'</span> <span class="string">'http://www.flybi.net/blog/seng/3599'</span>  sengblog.pdf</span><br><span class="line"></span><br><span class="line">wkhtmltopdf  --javascript-delay <span class="number">2000</span> <span class="string">'http://www.flybi.net/blog/seng/3645'</span> <span class="string">'http://www.flybi.net/blog/seng/3599'</span>  sengblog.pdf</span><br><span class="line"></span><br><span class="line">wkhtmltopdf  --dump-outline out.xsl  toc <span class="string">'http://www.flybi.net/blog/seng/3645'</span> <span class="string">'http://www.flybi.net/blog/seng/3599'</span>  sengblog.pdf</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//ask.hellobi.com/blog/seng/3691</span></span><br></pre></td></tr></table></figure>
<h3 id="如何批量添加图片水印"><a href="#如何批量添加图片水印" class="headerlink" title="如何批量添加图片水印"></a>如何批量添加图片水印</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">python 利用opencv去除图片水印 https:<span class="comment">//mp.weixin.qq.com/s/BqeBk0oPP1KpueviCwZFFQ</span></span><br><span class="line"></span><br><span class="line">https:<span class="comment">//mp.weixin.qq.com/s/QnMzvq_VWs2HyKHhD4FxQg</span></span><br><span class="line"><span class="keyword">import</span> os,traceback</span><br><span class="line"><span class="keyword">from</span> PIL <span class="keyword">import</span> Image</span><br><span class="line"></span><br><span class="line"># 获取文件夹图片</span><br><span class="line">def get_folder(fpath,wm_file,save_path):</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        img_suffix_list = [<span class="string">'png'</span>, <span class="string">'jpg'</span>, <span class="string">'bmp'</span>]</span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> os.listdir(fpath):</span><br><span class="line">            <span class="keyword">if</span> i.split(<span class="string">'.'</span>)[<span class="number">-1</span>] <span class="keyword">in</span> img_suffix_list:</span><br><span class="line">                img_path = fpath + <span class="string">'/'</span> + i</span><br><span class="line">                img_water_mark(img_file=img_path,wm_file=wm_file,save_path=save_path)</span><br><span class="line">    except Exception <span class="keyword">as</span> e:</span><br><span class="line">        print(traceback.print_exc())</span><br><span class="line"></span><br><span class="line"># 图片添加水印</span><br><span class="line">def img_water_mark(img_file, wm_file,save_path):</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        img = Image.open(img_file)  # 打开图片</span><br><span class="line">        watermark = Image.open(wm_file)  # 打开水印</span><br><span class="line">        img_size = img.size</span><br><span class="line">        wm_size = watermark.size</span><br><span class="line">        # 如果图片大小小于水印大小</span><br><span class="line">        <span class="keyword">if</span> img_size[<span class="number">0</span>] &amp;lt; wm_size[<span class="number">0</span>]:</span><br><span class="line">            watermark.resize(tuple(map(lambda x: int(x * <span class="number">0.5</span>), watermark.size)))</span><br><span class="line">        print(<span class="string">'图片大小：'</span>, img_size)</span><br><span class="line">        wm_position = (img_size[0]-wm_size[0],img_size[1]-wm_size[1]) # 默认设定水印位置为右下角</span><br><span class="line">        layer = Image.new('RGBA', img.size)  # 新建一个图层</span><br><span class="line">        layer.paste(watermark, wm_position)  # 将水印图片添加到图层上</span><br><span class="line">        mark_img = Image.composite(layer, img, layer)</span><br><span class="line">        new_file_name = <span class="string">'/new_'</span>+img_file.split(<span class="string">'/'</span>)[<span class="number">-1</span>]</span><br><span class="line">        mark_img.save(save_path + new_file_name)</span><br><span class="line">    except Exception <span class="keyword">as</span> e:</span><br><span class="line">        print(traceback.print_exc())</span><br></pre></td></tr></table></figure>
<h3 id="爬虫简书"><a href="#爬虫简书" class="headerlink" title="爬虫简书"></a>爬虫简书</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> lxml <span class="keyword">import</span> etree</span><br><span class="line"></span><br><span class="line">my_header = <span class="string">"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"</span></span><br><span class="line"></span><br><span class="line">res = requests.get(url=<span class="string">'https://www.jianshu.com/users/5aa8494a18c8/timeline'</span>, headers=&#123;<span class="string">'user-agent'</span>: my_header&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> <span class="string">'666'</span> <span class="keyword">in</span> res.text:</span><br><span class="line">    print(<span class="string">'found'</span>)</span><br><span class="line">page = etree.HTML(res.text)</span><br><span class="line">last_li = page.xpath(<span class="string">''</span><span class="string">'//ul[@class="note-list"]/li[last()]'</span><span class="string">''</span>)[<span class="number">0</span>]</span><br><span class="line">max_id = int(last_li.get(<span class="string">'id'</span>).split(<span class="string">'-'</span>)[<span class="number">1</span>]) - <span class="number">1</span></span><br><span class="line"></span><br><span class="line">file = open(<span class="string">"activity.txt"</span>,<span class="string">'w'</span>,encoding=<span class="string">'utf-8'</span>)</span><br><span class="line"></span><br><span class="line">page = <span class="number">2</span></span><br><span class="line"><span class="keyword">while</span> True:</span><br><span class="line">    res = requests.get(url=<span class="string">'https://www.jianshu.com/users/5aa8494a18c8/timeline?max_id=%s&amp;page=%s'</span> %(max_id,page),</span><br><span class="line">        headers=&#123;<span class="string">'user-agent'</span>: my_header, <span class="string">'X-INFINITESCROLL'</span>:<span class="string">'true'</span>&#125;)</span><br><span class="line"></span><br><span class="line">    last_li = etree.HTML(res.text).xpath(<span class="string">''</span><span class="string">'/html/body/li[last()]'</span><span class="string">''</span>)[<span class="number">0</span>]</span><br><span class="line">    max_id = int(last_li.get(<span class="string">'id'</span>).split(<span class="string">'-'</span>)[<span class="number">1</span>]) - <span class="number">1</span></span><br><span class="line">    page = page + <span class="number">1</span></span><br><span class="line">    file.write(res.text)</span><br><span class="line">    file.write(<span class="string">"\n"</span>)</span><br><span class="line">    <span class="keyword">if</span> <span class="string">'加入了简书'</span> <span class="keyword">in</span> res.text:</span><br><span class="line">        print(<span class="string">'end'</span>)</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line">file.close()</span><br><span class="line">https:<span class="comment">//www.jianshu.com/p/35a85ee14f7b</span></span><br></pre></td></tr></table></figure>
<h3 id="markdown-转PDF"><a href="#markdown-转PDF" class="headerlink" title="markdown 转PDF"></a>markdown 转PDF</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br></pre></td><td class="code"><pre><span class="line">pip install markdown </span><br><span class="line">markdown.markdown() 函数就可以读取 md 文件里的内容了</span><br><span class="line">先转HTML </span><br><span class="line"><span class="keyword">import</span> markdown</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> codecs</span><br><span class="line"><span class="string">''</span><span class="string">'</span></span><br><span class="line"><span class="string">savepath = "F:\RenZhengfei"</span></span><br><span class="line"><span class="string">os.chdir(savepath)</span></span><br><span class="line"><span class="string">file = codecs.open("README.md",  mode="r", encoding="utf-8")</span></span><br><span class="line"><span class="string">text = file.read()</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">html = markdown.markdown(text)</span></span><br><span class="line"><span class="string">print(html)</span></span><br><span class="line"><span class="string">with open('</span>file_name.html<span class="string">', '</span>w<span class="string">') as f:</span></span><br><span class="line"><span class="string">    f.write(html)</span></span><br><span class="line"><span class="string">'</span><span class="string">''</span></span><br><span class="line"></span><br><span class="line">head = <span class="string">""</span><span class="string">"&lt;!DOCTYPE html&gt;</span></span><br><span class="line"><span class="string">&lt;html&gt;</span></span><br><span class="line"><span class="string">&lt;head&gt;</span></span><br><span class="line"><span class="string">&lt;meta http-equiv="</span>Content-Type<span class="string">" content="</span>text/html;charset=utf<span class="number">-8</span><span class="string">" /&gt;</span></span><br><span class="line"><span class="string">&lt;style type="</span>text/css<span class="string">"&gt;</span></span><br><span class="line"><span class="string">code &#123;</span></span><br><span class="line"><span class="string">  color: inherit;</span></span><br><span class="line"><span class="string">  background-color: rgba(0, 0, 0, 0.05);</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">&lt;/style&gt;</span></span><br><span class="line"><span class="string">&lt;/head&gt;</span></span><br><span class="line"><span class="string">&lt;body&gt;</span></span><br><span class="line"><span class="string">"</span><span class="string">""</span></span><br><span class="line"></span><br><span class="line">foot = <span class="string">""</span><span class="string">"</span></span><br><span class="line"><span class="string">&lt;/body&gt;</span></span><br><span class="line"><span class="string">&lt;/html&gt;</span></span><br><span class="line"><span class="string">"</span><span class="string">""</span></span><br><span class="line">filepath = <span class="string">"F:\RenZhengfei-master\ALL"</span></span><br><span class="line">savepath = <span class="string">"F:\RenZhengfei-master\ALL-html"</span></span><br><span class="line"><span class="keyword">if</span> not os.path.isdir(savepath):</span><br><span class="line">    os.mkdir(savepath)</span><br><span class="line">os.chdir(savepath)</span><br><span class="line"></span><br><span class="line">i = <span class="number">0</span></span><br><span class="line">pathDir = os.listdir(filepath)</span><br><span class="line"><span class="keyword">for</span> allDir <span class="keyword">in</span> pathDir:</span><br><span class="line">    <span class="keyword">if</span> (allDir == <span class="string">"pdf"</span>):</span><br><span class="line">        <span class="keyword">continue</span></span><br><span class="line">    name = allDir</span><br><span class="line">    print(name)</span><br><span class="line"></span><br><span class="line">    os.chdir(filepath)</span><br><span class="line">    fp1 = codecs.open(name, mode=<span class="string">"r"</span>, encoding=<span class="string">"utf-8"</span>)</span><br><span class="line">    text = fp1.read()</span><br><span class="line">    html = markdown.markdown(text)</span><br><span class="line">    fp1.close()</span><br><span class="line">    #print(html)</span><br><span class="line"></span><br><span class="line">    fname = name.replace(<span class="string">'md'</span>, <span class="string">'html'</span>)</span><br><span class="line"></span><br><span class="line">    #f2 = '%s.html' % (fname)</span><br><span class="line">    os.chdir(savepath)</span><br><span class="line">    fp2 = codecs.open(fname, <span class="string">"w"</span>, encoding=<span class="string">"utf-8"</span>, errors=<span class="string">"xmlcharrefreplace"</span>)</span><br><span class="line">    fp2.write(head + html + foot)</span><br><span class="line">    fp2.close()</span><br><span class="line"></span><br><span class="line">print(i)</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//wemp.app/posts/6f807ecf-9ebd-4449-b419-2cfbf8c2e41f</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> pdfkit</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line">wk_path = r<span class="string">'E:\Program Files\wkhtmltox\bin\wkhtmltopdf.exe'</span></span><br><span class="line">config = pdfkit.configuration(wkhtmltopdf=wk_path)</span><br><span class="line"></span><br><span class="line">filepath = <span class="string">"F:\RenZhengfei-master\ALL-html"</span></span><br><span class="line">savepath = <span class="string">"F:\RenZhengfei-master\ALL-pdf"</span></span><br><span class="line">time1 = time.time()</span><br><span class="line">pathDir = os.listdir(filepath)</span><br><span class="line"><span class="keyword">for</span> allDir <span class="keyword">in</span> pathDir:</span><br><span class="line">    <span class="keyword">if</span> (allDir == <span class="string">"pdf"</span>):</span><br><span class="line">        <span class="keyword">continue</span></span><br><span class="line">    name = allDir</span><br><span class="line">    print(name)</span><br><span class="line">    htmlpath=filepath+<span class="string">"\\"</span>+name</span><br><span class="line">    print(htmlpath)</span><br><span class="line">    name = name.replace(<span class="string">'html'</span>, <span class="string">'pdf'</span>)</span><br><span class="line">    os.chdir(savepath)</span><br><span class="line">    pdfkit.from_url(htmlpath, name, configuration=config)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#pdfkit.from_url(url, name, configuration=config)</span><br><span class="line">time2 = time.time()</span><br><span class="line">print(str(time2 - time1)+<span class="string">" s"</span>)</span><br></pre></td></tr></table></figure>
<h3 id="验证身份证号"><a href="#验证身份证号" class="headerlink" title="验证身份证号"></a>验证身份证号</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line">#生成出生当年所有日期</span><br><span class="line">def dateRange(year):</span><br><span class="line">    fmt = <span class="string">'%Y-%m-%d'</span></span><br><span class="line">    bgn = int(time.mktime(time.strptime(year+<span class="string">'-01-01'</span>,fmt)))</span><br><span class="line">    end = int(time.mktime(time.strptime(year+<span class="string">'-12-31'</span>,fmt)))</span><br><span class="line">    list_date = [time.strftime(fmt,time.localtime(i)) <span class="keyword">for</span> i <span class="keyword">in</span> range(bgn,end+<span class="number">1</span>,<span class="number">3600</span>*<span class="number">24</span>)]</span><br><span class="line">    <span class="keyword">return</span> [i.replace(<span class="string">'-'</span>,<span class="string">''</span>) <span class="keyword">for</span> i <span class="keyword">in</span> list_date]</span><br><span class="line"></span><br><span class="line">data_time  = dateRange(<span class="string">'1993'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> id_validator <span class="keyword">import</span> validator</span><br><span class="line"></span><br><span class="line">#遍历所有日期，print通过校验的身份证号码</span><br><span class="line">pip install id-validator</span><br><span class="line">def vali_dator(id1,id2,id3):</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> dateRange(id2):</span><br><span class="line">        theid = id1 + i + id3</span><br><span class="line">        <span class="keyword">if</span> validator.is_valid(theid):</span><br><span class="line">            print(theid)</span><br><span class="line"></span><br><span class="line">vali_dator(<span class="string">'330221'</span>,<span class="string">'1993'</span>,<span class="string">'4914'</span>)</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//mp.weixin.qq.com/s?__biz=MzU5MjI3NzIxMw==&amp;mid=2247486816&amp;idx=1&amp;sn=baa976db515e3b9b99e7001daa9a577a&amp;chksm=fe2376d2c954ffc486625e5420e3ebcf3d83581986b0568b804fb5a54e4aaa032b4992c13905&amp;mpshare=1&amp;scene=1&amp;srcid=1023PX0DRWmDc5E8oEZSVUx6&amp;sharer_sharetime=1571795782903&amp;sharer_shareid=43165518fc08bc947dca48788293333a&amp;key=6f23511bf9e1c01f4c78d4f8f46e1b1e8fc6e548405a6029e3b015de7441c1527cd4817fc238470a3211f36f03178e6f7f9888d5f7d1ee5e6ef6b0b0fced5da2f45aa739e184ae5749a86f5102efd4f9&amp;ascene=1&amp;uin=NjQ3OTQwMTAy&amp;devicetype=Windows+7&amp;version=62070152&amp;lang=zh_CN&amp;pass_ticket=Nl73k%2FpmXYhrLnAbsjSStmagh1FEZZkB8fhtyVf9%2BmzY8foNNpPw%2FmaVHa2zPKdu</span></span><br><span class="line">#print(validator.get_info('330221199306084914'))</span><br><span class="line">https:<span class="comment">//github.com/zpw1995/aotodata/blob/master/interest/ID_card/ID_card.py</span></span><br></pre></td></tr></table></figure>
<h3 id="NumPy-基础"><a href="#NumPy-基础" class="headerlink" title="NumPy 基础"></a>NumPy 基础</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">NumPy 的主要对象是多维数组 Ndarray。在 NumPy 中维度（dimensions）叫做轴（axes），轴的个数叫做秩（rank）</span><br><span class="line">&gt;&gt;&gt; np.array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>])</span><br><span class="line">array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>])</span><br><span class="line">&gt;&gt;&gt; np.array([(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>), (<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>)])</span><br><span class="line">array([[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">       [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]])</span><br><span class="line">&gt;&gt;&gt; np.zeros((<span class="number">3</span>, <span class="number">3</span>))</span><br><span class="line">array([[<span class="number">0.</span>, <span class="number">0.</span>, <span class="number">0.</span>],</span><br><span class="line">       [<span class="number">0.</span>, <span class="number">0.</span>, <span class="number">0.</span>],</span><br><span class="line">       [<span class="number">0.</span>, <span class="number">0.</span>, <span class="number">0.</span>]])</span><br><span class="line">&gt;&gt;&gt; np.arange(<span class="number">5</span>)</span><br><span class="line">array([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>])</span><br><span class="line">&gt;&gt;&gt; np.arange(<span class="number">6</span>).reshape(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line">array([[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>],</span><br><span class="line">       [<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]])</span><br><span class="line">    &gt;&gt;&gt; np.random.rand(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line">    array([[<span class="number">0.50122984</span>, <span class="number">0.98824375</span>, <span class="number">0.81388012</span>],</span><br><span class="line">           [<span class="number">0.60951775</span>, <span class="number">0.02055326</span>, <span class="number">0.97622093</span>]])   </span><br><span class="line">   &gt;&gt;&gt; np.random.randint(<span class="number">5</span>, size=(<span class="number">2</span>, <span class="number">3</span>))</span><br><span class="line">   array([[<span class="number">2</span>, <span class="number">0</span>, <span class="number">2</span>],</span><br><span class="line">          [<span class="number">4</span>, <span class="number">4</span>, <span class="number">4</span>]])</span><br><span class="line">          </span><br><span class="line">   https:<span class="comment">//learnku.com/articles/35684</span></span><br><span class="line"> &gt;&gt;&gt; a = np.array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>])</span><br><span class="line"> &gt;&gt;&gt; b = np.arange(<span class="number">1</span>, <span class="number">6</span>)</span><br><span class="line"> &gt;&gt;&gt; a, b</span><br><span class="line"> (array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]), array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]))</span><br></pre></td></tr></table></figure>
<p>###<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line">爬取豆瓣电影TOP250</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line"></span><br><span class="line">url = <span class="string">'https://movie.douban.com/top250'</span></span><br><span class="line"># 使用U-A伪装成浏览器发送请求</span><br><span class="line">headers = &#123;<span class="string">'User-Agent'</span>: <span class="string">'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'</span>&#125;</span><br><span class="line"># 先使用requests发送网络请求从而获取网页</span><br><span class="line">r = requests.get(<span class="string">'https://movie.douban.com/top250'</span>, headers=headers)</span><br><span class="line"># 使用bs4解析获取的网页</span><br><span class="line">soup = BeautifulSoup(r.text, <span class="string">'html.parser'</span>)</span><br><span class="line"># 调用prettify()方法来使解析的HTML更加规范化</span><br><span class="line">print(soup.prettify())</span><br><span class="line">movie_list = soup.find('ol', attrs=&#123;'class': 'grid_view'&#125;) #电影列表</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> movie <span class="keyword">in</span> movie_list.find_all(<span class="string">'li'</span>):</span><br><span class="line">    movie_name = movie.find(<span class="string">'span'</span>, attrs=&#123;<span class="string">'class'</span>: <span class="string">'title'</span>&#125;)</span><br><span class="line">    print(movie_name.get_text())</span><br><span class="line">    </span><br><span class="line">肖申克的救赎</span><br><span class="line">霸王别姬</span><br><span class="line">这个杀手不太冷</span><br><span class="line">阿甘正传</span><br><span class="line">美丽人生</span><br><span class="line"><span class="keyword">import</span> codecs</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line"></span><br><span class="line">DOWNLOAD_URL = <span class="string">'https://movie.douban.com/top250'</span></span><br><span class="line"></span><br><span class="line">def download_page(url):</span><br><span class="line">    <span class="keyword">return</span> requests.get(url, headers=&#123;</span><br><span class="line">        <span class="string">'User-Agent'</span>:<span class="string">'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'</span></span><br><span class="line">    &#125;).content</span><br><span class="line"></span><br><span class="line">def parse_html(html):</span><br><span class="line">    soup = BeautifulSoup(html, <span class="string">'html.parser'</span>)</span><br><span class="line">    # 电影列表</span><br><span class="line">    movie_list_soup = soup.find(<span class="string">'ol'</span>, attrs=&#123;<span class="string">'class'</span>: <span class="string">'grid_view'</span>&#125;)</span><br><span class="line">    movie_name_list = []</span><br><span class="line">    <span class="keyword">for</span> movie_li <span class="keyword">in</span> movie_list_soup.find_all(<span class="string">'li'</span>):</span><br><span class="line">        movie_name = movie_li.find(<span class="string">'span'</span>, attrs=&#123;<span class="string">'class'</span>: <span class="string">'title'</span>&#125;).get_text()</span><br><span class="line">        movie_info = movie_li.find(<span class="string">'div'</span>, attrs=&#123;<span class="string">'class'</span>: <span class="string">'bd'</span>&#125;).find(<span class="string">'p'</span>).get_text()</span><br><span class="line">        movie_star = movie_li.find(<span class="string">'span'</span>, attrs=&#123;<span class="string">'class'</span>: <span class="string">'rating_num'</span>&#125;).get_text()</span><br><span class="line">        movie_name_list.append(movie_name)</span><br><span class="line">        movie_name_list.append(movie_info)</span><br><span class="line">        movie_name_list.append(movie_star)</span><br><span class="line">    # 下一页链接</span><br><span class="line">    next_page = soup.find(<span class="string">'span'</span>, attrs=&#123;<span class="string">'class'</span>: <span class="string">'next'</span>&#125;).find(<span class="string">'a'</span>)</span><br><span class="line">    <span class="keyword">if</span> next_page:</span><br><span class="line">        <span class="keyword">return</span> movie_name_list,DOWNLOAD_URL + next_page[<span class="string">'href'</span>]</span><br><span class="line">    <span class="keyword">return</span> movie_name_list, None</span><br><span class="line"></span><br><span class="line">def main():</span><br><span class="line">    url = DOWNLOAD_URL</span><br><span class="line">    <span class="keyword">with</span> codecs.open(<span class="string">'movies'</span>,<span class="string">'wb'</span>, encoding=<span class="string">'utf-8'</span>) <span class="keyword">as</span> f:</span><br><span class="line">        <span class="keyword">while</span> True:</span><br><span class="line">            html = download_page(url)</span><br><span class="line">            movies, url =parse_html(html)</span><br><span class="line">            f.write(u<span class="string">'&#123;movies&#125;\n'</span>.format(movies=<span class="string">'\n'</span>.join(movies)))</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    main()</span><br><span class="line">https:<span class="comment">//www.jianshu.com/p/8a460be5a26e</span></span><br></pre></td></tr></table></figure></p>
<h3 id="把bmp和png转换成jpg"><a href="#把bmp和png转换成jpg" class="headerlink" title="把bmp和png转换成jpg"></a>把bmp和png转换成jpg</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">from</span> PIL <span class="keyword">import</span> Image</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> root, dirs, files <span class="keyword">in</span> os.walk(<span class="string">"."</span>):</span><br><span class="line">    <span class="keyword">for</span> bmpfig <span class="keyword">in</span> files:</span><br><span class="line">        <span class="keyword">if</span> not bmpfig.endswith(<span class="string">'.bmp'</span>) and not bmpfig.endswith(<span class="string">'.png'</span>):</span><br><span class="line">            <span class="keyword">continue</span></span><br><span class="line">        bmpfig = os.path.join(root, bmpfig)</span><br><span class="line">        newfigname = bmpfig[:<span class="number">-4</span>] + <span class="string">".jpg"</span></span><br><span class="line">        print <span class="string">"converting from"</span>, bmpfig, <span class="string">"to"</span>, newfigname</span><br><span class="line">        img = Image.open(bmpfig)</span><br><span class="line">        img = img.convert('RGB')  # for png</span><br><span class="line">        img.save(newfigname, format=<span class="string">'jpeg'</span>, quality=<span class="number">95</span>)</span><br><span class="line">        img.close()</span><br><span class="line">        os.remove(bmpfig)</span><br><span class="line">https:<span class="comment">//zjyfdu.github.io/2018/08/16/python%E6%8A%8Abmp%E8%BD%AC%E6%8D%A2%E6%88%90jpg/</span></span><br></pre></td></tr></table></figure>
<h3 id="numpy"><a href="#numpy" class="headerlink" title="numpy"></a>numpy</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line">&gt;&gt;&gt; print(np.__version__)</span><br><span class="line"><span class="number">1.16</span><span class="number">.2</span></span><br><span class="line">&gt;&gt;&gt; np.array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>])</span><br><span class="line">array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>])</span><br><span class="line">&gt;&gt;&gt; np.arange(<span class="number">5</span>)</span><br><span class="line">array([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>])</span><br><span class="line">&gt;&gt;&gt; np.arange(<span class="number">6</span>).reshape(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line">array([[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>],</span><br><span class="line">       [<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]])</span><br><span class="line">&gt;&gt;&gt; np.random.rand(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line">array([[<span class="number">0.50122984</span>, <span class="number">0.98824375</span>, <span class="number">0.81388012</span>],</span><br><span class="line">       [<span class="number">0.60951775</span>, <span class="number">0.02055326</span>, <span class="number">0.97622093</span>]])</span><br><span class="line">  &gt;&gt;&gt; np.random.randint(<span class="number">5</span>, size=(<span class="number">2</span>, <span class="number">3</span>))</span><br><span class="line">  array([[<span class="number">2</span>, <span class="number">0</span>, <span class="number">2</span>],</span><br><span class="line">         [<span class="number">4</span>, <span class="number">4</span>, <span class="number">4</span>]])     </span><br><span class="line">       </span><br><span class="line">  &gt;&gt;&gt; a = np.array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>])</span><br><span class="line">  &gt;&gt;&gt; b = np.arange(<span class="number">1</span>, <span class="number">6</span>)     </span><br><span class="line">  &gt;&gt;&gt; a + b </span><br><span class="line">  array([ <span class="number">2</span>,  <span class="number">4</span>,  <span class="number">6</span>,  <span class="number">8</span>, <span class="number">10</span>])     </span><br><span class="line">   &gt;&gt;&gt; np.sin(a)</span><br><span class="line">   array([<span class="number">-0.54402111</span>,  <span class="number">0.91294525</span>, <span class="number">-0.98803162</span>,  <span class="number">0.74511316</span>, <span class="number">-0.26237485</span>])</span><br><span class="line">   </span><br><span class="line">   &gt;&gt;&gt; np.sqrt(a)</span><br><span class="line">   array([<span class="number">3.16227766</span>, <span class="number">4.47213595</span>, <span class="number">5.47722558</span>, <span class="number">6.32455532</span>, <span class="number">7.07106781</span>])</span><br><span class="line">   &gt;&gt;&gt; a ** 0.5 # 等价于np.sqrt(a)</span><br><span class="line">   array([<span class="number">3.16227766</span>, <span class="number">4.47213595</span>, <span class="number">5.47722558</span>, <span class="number">6.32455532</span>, <span class="number">7.07106781</span>])  </span><br><span class="line">   &gt;&gt;&gt; np.power(a, <span class="number">3</span>)</span><br><span class="line">   array([  <span class="number">1000</span>,   <span class="number">8000</span>,  <span class="number">27000</span>,  <span class="number">64000</span>, <span class="number">125000</span>])</span><br><span class="line">   &gt;&gt;&gt; a ** 3 # 等价于np.power(a, 3)  </span><br><span class="line">   https:<span class="comment">//learnku.com/articles/35686</span></span><br></pre></td></tr></table></figure>
<h3 id="pdf-to-image"><a href="#pdf-to-image" class="headerlink" title="pdf to image"></a>pdf to image</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//github.com/freedesktop/poppler  </span></span><br><span class="line">pdftoppm -singlefile -f <span class="number">4</span> -r <span class="number">72</span> -jpeg -jpegopt quality=<span class="number">90</span> presentation.pdf test_poppler</span><br><span class="line"></span><br><span class="line">pdftoppm  -f <span class="number">1</span> -r <span class="number">72</span> -jpeg -jpegopt quality=<span class="number">90</span> test_20191120_134947.pdf test_poppler</span><br><span class="line">转换所有 生成多个图片</span><br><span class="line"><span class="keyword">from</span> pdf2image <span class="keyword">import</span> convert_from_path</span><br><span class="line"></span><br><span class="line">def main():</span><br><span class="line">    pages = convert_from_path(<span class="string">"presentation.pdf"</span>, first_page=<span class="number">2</span>,</span><br><span class="line">                              single_file=True)</span><br><span class="line">    pages[<span class="number">0</span>].save(<span class="string">"test_pdf2image.jpg"</span>, quality=<span class="number">85</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">"__main__"</span>:</span><br><span class="line">    main()</span><br><span class="line">https:<span class="comment">//jdhao.github.io/2019/11/14/convert_pdf_to_images_pdftoppm/</span></span><br><span class="line">https:<span class="comment">//imagemagick.org/script/download.php</span></span><br><span class="line">convert -density <span class="number">150</span> presentation.pdf -quality <span class="number">90</span> output-%<span class="number">3</span>d.jpg</span><br><span class="line">https:<span class="comment">//jdhao.github.io/2019/11/20/convert_pdf_to_image_imagemagick/#convert-all-pages-of-pdf-file-to-images</span></span><br></pre></td></tr></table></figure>
<h3 id="图片exif"><a href="#图片exif" class="headerlink" title="图片exif"></a>图片exif</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//www.irfanview.com/</span></span><br><span class="line"><span class="keyword">from</span> PIL <span class="keyword">import</span> Image</span><br><span class="line"><span class="keyword">from</span> PIL. from PIL.ExifTags <span class="keyword">import</span> TAGS</span><br><span class="line"></span><br><span class="line">img = Image.open(<span class="string">'test.jpg'</span>)</span><br><span class="line"></span><br><span class="line">exif = img.getexif()</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> k, v <span class="keyword">in</span> exif.items():</span><br><span class="line">    print(<span class="string">'&#123;&#125;: &#123;&#125;'</span>.format(TAGS[k], v))</span><br><span class="line"> <span class="keyword">from</span> PIL <span class="keyword">import</span> Image</span><br><span class="line"> <span class="keyword">import</span> piexif</span><br><span class="line"> </span><br><span class="line"> img = Image.open(<span class="string">'test.jpg'</span>)</span><br><span class="line"> <span class="keyword">if</span> <span class="string">"exif"</span> <span class="keyword">in</span> img.info:</span><br><span class="line">     exif_dict = piexif.load(img.info[<span class="string">'exif'</span>])</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">if</span> piexif.ImageIFD.Orientation <span class="keyword">in</span> exif_dict[<span class="string">'0th'</span>]:</span><br><span class="line">     exif_dict[<span class="string">'0th'</span>][pixeif.ImageIFD.Orientation] = <span class="number">3</span></span><br><span class="line"> </span><br><span class="line">     # quick and dirty work around to avoid type error</span><br><span class="line">     exif_dict[<span class="string">'Exif'</span>][<span class="number">41729</span>] = b<span class="string">'1'</span></span><br><span class="line"> </span><br><span class="line">     exif_bytes = piexif.dump(exif_dict)</span><br><span class="line"> </span><br><span class="line"> img.save(<span class="string">'new_img.jpg'</span>, exif=exif_bytes)   </span><br><span class="line">https:<span class="comment">//jdhao.github.io/2019/07/31/image_rotation_exif_info/</span></span><br></pre></td></tr></table></figure>
<h3 id="命令行里处理数据科学问题"><a href="#命令行里处理数据科学问题" class="headerlink" title="命令行里处理数据科学问题"></a>命令行里处理数据科学问题</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line">curl -o data_dl.csv https:<span class="comment">//archive.ics.uci.edu/ml/machine-learning-databases/blood-transfusion/transfusion.data</span></span><br><span class="line">Recency (months),Frequency (times),Monetary (c.c. blood),Time (months),<span class="string">"whether he/she donated blood in March 2007"</span></span><br><span class="line"><span class="number">2</span> ,<span class="number">50</span>,<span class="number">12500</span>,<span class="number">98</span> ,<span class="number">1</span></span><br><span class="line"><span class="number">0</span> ,<span class="number">13</span>,<span class="number">3250</span>,<span class="number">28</span> ,<span class="number">1</span></span><br><span class="line"><span class="number">1</span> ,<span class="number">16</span>,<span class="number">4000</span>,<span class="number">35</span> ,<span class="number">1</span></span><br><span class="line"><span class="number">2</span> ,<span class="number">20</span>,<span class="number">5000</span>,<span class="number">45</span> ,<span class="number">1</span></span><br><span class="line"><span class="number">1</span> ,<span class="number">24</span>,<span class="number">6000</span>,<span class="number">77</span> ,<span class="number">0</span></span><br><span class="line"></span><br><span class="line">pip install csvkit</span><br><span class="line">csvclean data_dl.csv</span><br><span class="line">csvcut -n data_dl_out.csv | cut -c6-</span><br><span class="line">Recency (months)</span><br><span class="line">Frequency (times)</span><br><span class="line">Monetary (c.c. blood)</span><br><span class="line">Time (months)</span><br><span class="line">whether he/she donated blood <span class="keyword">in</span> March <span class="number">2007</span></span><br><span class="line"></span><br><span class="line">csvstat --mean data_dl_out.csv</span><br><span class="line"><span class="number">1.</span> a: <span class="number">373.5</span></span><br><span class="line"><span class="number">2.</span> Recency (months): <span class="number">9.507</span></span><br><span class="line"><span class="number">3.</span> Frequency (times): <span class="number">5.515</span></span><br><span class="line"><span class="number">4.</span> Monetary (c.c. blood): <span class="number">1</span>,<span class="number">378.676</span></span><br><span class="line"><span class="number">5.</span> Time (months): <span class="number">34.282</span></span><br><span class="line"><span class="number">6.</span> whether he/she donated blood <span class="keyword">in</span> March <span class="number">2007</span>: None</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line">data = pd.read_csv(<span class="string">'data_dl_out.csv'</span>)</span><br><span class="line">data.head()</span><br><span class="line"></span><br><span class="line">data = data.rename(columns=&#123;<span class="string">'Recency (months)'</span>: <span class="string">'recency'</span>,</span><br><span class="line">             <span class="string">'Frequency (times)'</span>: <span class="string">'frequency'</span>,</span><br><span class="line">             <span class="string">'Monetary (c.c. blood)'</span>: <span class="string">'volumne'</span>,</span><br><span class="line">             <span class="string">'Time (months)'</span>: <span class="string">'time'</span>,</span><br><span class="line">             <span class="string">'whether he/she donated blood in March 2007'</span>: <span class="string">'target'</span>&#125;)</span><br><span class="line">data.to_csv(<span class="string">'data_clean.csv'</span>)</span><br><span class="line"></span><br><span class="line">	recency	frequency	volumne	time	target</span><br><span class="line"><span class="number">0</span>	<span class="number">2</span>	<span class="number">50</span>	<span class="number">12500</span>	<span class="number">98</span>	<span class="number">1</span></span><br><span class="line"><span class="number">1</span>	<span class="number">0</span>	<span class="number">13</span>	<span class="number">3250</span>	<span class="number">28</span>	<span class="number">1</span></span><br><span class="line"><span class="number">2</span>	<span class="number">1</span>	<span class="number">16</span>	<span class="number">4000</span>	<span class="number">35</span>	<span class="number">1</span></span><br><span class="line"><span class="number">3</span>	<span class="number">2</span>	<span class="number">20</span>	<span class="number">5000</span>	<span class="number">45</span>	<span class="number">1</span></span><br><span class="line"></span><br><span class="line">csvsql --query  <span class="string">"select frequency, count(*) as rows from data_clean where target = 1 group by frequency order by 2 desc"</span> data_clean.csv</span><br><span class="line">d:\python\lib\site-packages\win32\lib\pywintypes.py:<span class="number">2</span>: DeprecationWarning: the imp <span class="built_in">module</span> is deprecated <span class="keyword">in</span> favour <span class="keyword">of</span> importlib; see the <span class="built_in">module</span><span class="string">'s documentation for alternative uses</span></span><br><span class="line"><span class="string">d:\python\lib\site-packages\agate\utils.py:276: UnnamedColumnWarning: Column 0 has no name. Using "a".</span></span><br><span class="line"><span class="string">frequency,rows</span></span><br><span class="line"><span class="string">1.0,20</span></span><br><span class="line"><span class="string">5.0,20</span></span><br><span class="line"><span class="string">2.0,19</span></span><br><span class="line"><span class="string">6.0,17</span></span><br><span class="line"><span class="string">https://oicebot.github.io/2019/07/25/five-command-line-tools-for-data-science.html</span></span><br></pre></td></tr></table></figure>
<h3 id="Pandas-做数据分析sql"><a href="#Pandas-做数据分析sql" class="headerlink" title="Pandas 做数据分析sql"></a>Pandas 做数据分析sql</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"></span><br><span class="line">url = (<span class="string">'https://raw.github.com/pandas-dev/pandas/master/pandas/tests/data/tips.csv'</span>)</span><br><span class="line">tips = pd.read_csv(url)</span><br><span class="line">output = tips.head()</span><br><span class="line">sql 语句： SELECT total_bill, tip, smoker, time FROM tips LIMIT <span class="number">5</span>;。</span><br><span class="line"></span><br><span class="line">output = tips[[<span class="string">'total_bill'</span>, <span class="string">'tip'</span>, <span class="string">'smoker'</span>, <span class="string">'time'</span>]].head(<span class="number">5</span>)</span><br><span class="line">   total_bill   tip smoker    time</span><br><span class="line"><span class="number">0</span>       <span class="number">16.99</span>  <span class="number">1.01</span>     No  Dinner</span><br><span class="line"><span class="number">1</span>       <span class="number">10.34</span>  <span class="number">1.66</span>     No  Dinner</span><br><span class="line"><span class="number">2</span>       <span class="number">21.01</span>  <span class="number">3.50</span>     No  Dinner</span><br><span class="line"><span class="number">3</span>       <span class="number">23.68</span>  <span class="number">3.31</span>     No  Dinner</span><br><span class="line"><span class="number">4</span>       <span class="number">24.59</span>  <span class="number">3.61</span>     No  Dinner</span><br><span class="line">sql 语句： SELECT * FROM tips WHERE time = <span class="string">'Dinner'</span> LIMIT <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line">output = tips[tips[<span class="string">'time'</span>] == <span class="string">'Dinner'</span>].head(<span class="number">5</span>)</span><br><span class="line"># 或者</span><br><span class="line">output = tips.query(<span class="string">"time == 'Dinner'"</span>).head(<span class="number">5</span>)</span><br><span class="line"></span><br><span class="line">sql 语句：SELECT * FROM tips WHERE time = <span class="string">'Dinner'</span>;。</span><br><span class="line"></span><br><span class="line">output = tips[(tips[<span class="string">'time'</span>] == <span class="string">'Dinner'</span>)]</span><br><span class="line">sql 语句：SELECT * FROM tips WHERE time = <span class="string">'Dinner'</span> AND tip &gt; <span class="number">5.00</span>;</span><br><span class="line"></span><br><span class="line">output = tips[(tips[<span class="string">'time'</span>] == <span class="string">'Dinner'</span>) &amp; (tips[<span class="string">'tip'</span>] &gt; <span class="number">5.00</span>)]</span><br><span class="line">sql 语句：SELECT * FROM tips WHERE size &gt;= <span class="number">5</span> OR total_bill &gt; <span class="number">45</span>;。</span><br><span class="line"></span><br><span class="line">output = tips[(tips[<span class="string">'size'</span>] &gt;= <span class="number">5</span>) | (tips[<span class="string">'total_bill'</span>] &gt; <span class="number">45</span>)]</span><br><span class="line">sql 语句：SELECT * FROM tips WHERE siez <span class="keyword">in</span> (<span class="number">5</span>, <span class="number">6</span>);。</span><br><span class="line"></span><br><span class="line">output = tips[tips[<span class="string">'size'</span>].isin([<span class="number">2</span>, <span class="number">5</span>])]</span><br><span class="line">sql 语句：SELECT sex, count(*) FROM tips GROUP BY sex;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/29825#replies</span></span><br><span class="line">output = tips.groupby(<span class="string">'sex'</span>).size()</span><br><span class="line">转数组</span><br><span class="line">&gt;&gt;&gt; tips.total_bill.head().tolist()</span><br><span class="line">[<span class="number">16.99</span>, <span class="number">10.34</span>, <span class="number">21.01</span>, <span class="number">23.68</span>, <span class="number">24.59</span>]</span><br><span class="line">&gt;&gt;&gt; tips.columns.tolist()</span><br><span class="line">[<span class="string">'total_bill'</span>, <span class="string">'tip'</span>, <span class="string">'sex'</span>, <span class="string">'smoker'</span>, <span class="string">'day'</span>, <span class="string">'time'</span>, <span class="string">'size'</span>]</span><br></pre></td></tr></table></figure>
<h3 id="在-Python-里用-is-进行比较"><a href="#在-Python-里用-is-进行比较" class="headerlink" title="在 Python 里用 is 进行比较"></a>在 Python 里用 is 进行比较</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> appointment.time_slot_id is time_slot.id:</span><br><span class="line">    time_slot_appointments.append(appointment)</span><br><span class="line">出问题的地方就是这个 is 啦。但最诡异的是，当 time_slot_id 和 time_slot.id 都是小于等于 <span class="number">256</span> 的整型数字时，这段代码一点问题都没有；只有当预约数量达到一定程度，使得 time_slot_id 或 time_slot.id 大于 <span class="number">256</span> 时，问题就出现了——这个表达式永远返回 False</span><br><span class="line"></span><br><span class="line">使用 is 关键字进行的比较是“引用比较”。这里的“引用”就相当于一个索引号，一个地址，或是指向一个对象的指针。用 is 进行比较正是造成这个奇怪 bug 的根源。</span><br><span class="line">使用 == 操作符进行的比较是“值比较”，也就是比较两个对象的“值”。</span><br><span class="line">在 Python 中，数值型的整型数据是以 PyObject 对象的一个子类型： PyLong 对象的形式存储的。为了减少内存管理在处理小整型数字时候的开销，在 CPython 解释器中使用了“小整数对象池”进行优化。也就是说，值为 <span class="number">-5</span> 到 <span class="number">256</span> 的 PyLong 对象已经预置在 CPython 解释器的私有堆中，可以通过 small_ints 这个数组进行访问。</span><br><span class="line">要想修复这个 bug，其中一种方式是，把：</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> appointment.time_slot_id is time_slot.id:</span><br><span class="line">改成：</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> appointment.time_slot.id is time_slot.id:</span><br><span class="line">只有当你十分确定要比较的是两个对象本身的时候，才用 is 进行比较。https:<span class="comment">//oicebot.github.io/2019/07/11/the-dangers-of-using-is-in-python.html</span></span><br></pre></td></tr></table></figure>
<h3 id="判断括号字符串"><a href="#判断括号字符串" class="headerlink" title="判断括号字符串"></a>判断括号字符串</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">def isValid(self, s):</span><br><span class="line">    stack = []</span><br><span class="line">    paren_map = &#123;<span class="string">')'</span>:<span class="string">'('</span>, <span class="string">']'</span>:<span class="string">'['</span>, <span class="string">'&#125;'</span>:<span class="string">'&#123;'</span>&#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> c <span class="keyword">in</span> s:</span><br><span class="line">        <span class="keyword">if</span> c not <span class="keyword">in</span> paren_map:</span><br><span class="line">            stack.apend(c)</span><br><span class="line">        elif not stack or paren[c] != stack.pop():</span><br><span class="line">            <span class="keyword">return</span> False</span><br><span class="line">    <span class="keyword">return</span> not stack</span><br></pre></td></tr></table></figure>
<h3 id="Could-not-fetch-URL-https-pypi-org-simple-pip-There-was-a-problem-confirming-the-ssl-certificate"><a href="#Could-not-fetch-URL-https-pypi-org-simple-pip-There-was-a-problem-confirming-the-ssl-certificate" class="headerlink" title="Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate"></a>Could not fetch URL <a href="https://pypi.org/simple/pip/" target="_blank" rel="noopener">https://pypi.org/simple/pip/</a>: There was a problem confirming the ssl certificate</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">pip install iredis</span><br><span class="line">WARNING: Retrying (Retry(total=<span class="number">4</span>, connect=None, read=None, redirect=None, status=None)) after connection broken by <span class="string">'SSLError(SSLCertVerificationError(1, '</span>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate <span class="keyword">in</span> certificate chain (_ssl.c:<span class="number">1045</span>)<span class="string">'))'</span>: <span class="regexp">/simple/i</span>redis/</span><br><span class="line">WARNING: Retrying (Retry(total=<span class="number">3</span>, connect=None, read=None, redirect=None, status=None)) after connection broken by <span class="string">'SSLError(SSLCertVerificationError(1, '</span>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate <span class="keyword">in</span> certificate chain (_ssl.c:<span class="number">1045</span>)<span class="string">'))'</span>: <span class="regexp">/simple/i</span>redis/</span><br><span class="line">WARNING: Retrying (Retry(total=<span class="number">2</span>, connect=None, read=None, redirect=None, status=None)) after connection broken by <span class="string">'SSLError(SSLCertVerificationError(1, '</span>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate <span class="keyword">in</span> certificate chain (_ssl.c:<span class="number">1045</span>)<span class="string">'))'</span>: <span class="regexp">/simple/i</span>redis/</span><br><span class="line">WARNING: Retrying (Retry(total=<span class="number">1</span>, connect=None, read=None, redirect=None, status=None)) after connection broken by <span class="string">'SSLError(SSLCertVerificationError(1, '</span>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate <span class="keyword">in</span> certificate chain (_ssl.c:<span class="number">1045</span>)<span class="string">'))'</span>: <span class="regexp">/simple/i</span>redis/</span><br><span class="line">WARNING: Retrying (Retry(total=<span class="number">0</span>, connect=None, read=None, redirect=None, status=None)) after connection broken by <span class="string">'SSLError(SSLCertVerificationError(1, '</span>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate <span class="keyword">in</span> certificate chain (_ssl.c:<span class="number">1045</span>)<span class="string">'))'</span>: <span class="regexp">/simple/i</span>redis/</span><br><span class="line">Could not fetch URL https:<span class="comment">//pypi.org/simple/iredis/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/iredis/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1045)'))) - skipping</span></span><br><span class="line">ERROR: Could not find a version that satisfies the requirement iredis (<span class="keyword">from</span> versions: none)</span><br><span class="line">ERROR: No matching distribution found <span class="keyword">for</span> iredis</span><br><span class="line">Could not fetch URL https:<span class="comment">//pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1045)'))) - skipping</span></span><br><span class="line">到https:<span class="comment">//pypi.python.org/pypi/pip#downloads下载 https://files.pythonhosted.org/packages/ce/ea/9b445176a65ae4ba22dce1d93e4b5fe182f953df71a145f557cffaffc1bf/pip-19.3.1.tar.gz</span></span><br><span class="line"></span><br><span class="line">解压出setup.py   执行python setup.py install</span><br><span class="line">Installing pip.exe script to D:\python\Scripts</span><br><span class="line">Installing pip.exe.manifest script to D:\python\Scripts</span><br><span class="line">Installing pip3-script.py script to D:\python\Scripts</span><br><span class="line">Installing pip3.exe script to D:\python\Scripts</span><br><span class="line">Installing pip3.exe.manifest script to D:\python\Scripts</span><br><span class="line">Installing pip3<span class="number">.7</span>-script.py script to D:\python\Scripts</span><br><span class="line">Installing pip3<span class="number">.7</span>.exe script to D:\python\Scripts</span><br><span class="line">Installing pip3<span class="number">.7</span>.exe.manifest script to D:\python\Scripts</span><br><span class="line"></span><br><span class="line">Installed d:\python\lib\site-packages\pip<span class="number">-19.3</span><span class="number">.1</span>-py3<span class="number">.7</span>.egg</span><br><span class="line">Processing dependencies <span class="keyword">for</span> pip==<span class="number">19.3</span><span class="number">.1</span></span><br><span class="line">Finished processing dependencies <span class="keyword">for</span> pip==<span class="number">19.3</span><span class="number">.1</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org iredis</span><br><span class="line">Collecting iredis</span><br><span class="line">  Downloading https:<span class="comment">//files.pythonhosted.org/packages/5c/1f/2da6df9c698a586f66bbb4153b7b2a75c62ce1e94aaf04ffaed1954163ad/iredis-0.7.0-py3-none-any.whl (42kB)</span></span><br><span class="line">     |████████████████████████████████| <span class="number">51</span>kB <span class="number">363</span>kB/s</span><br><span class="line">Collecting click8&lt;<span class="number">9</span>,&gt;=<span class="number">8</span></span><br></pre></td></tr></table></figure>
<h3 id="单行代码"><a href="#单行代码" class="headerlink" title="单行代码"></a>单行代码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"># 字典推导</span><br><span class="line">&#123;<span class="attr">v</span>: k <span class="keyword">for</span> k, v <span class="keyword">in</span> some_dict.items()&#125;</span><br><span class="line"># 集合推导</span><br><span class="line">&#123;x**<span class="number">2</span> <span class="keyword">for</span> x <span class="keyword">in</span> [<span class="number">1</span>, <span class="number">1</span>, <span class="number">2</span>]&#125;</span><br><span class="line"># 列表推导</span><br><span class="line">[i <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">30</span>) <span class="keyword">if</span> i % <span class="number">3</span> is <span class="number">0</span>]</span><br><span class="line">a_list = [[<span class="number">1</span>, <span class="number">2</span>], [<span class="number">3</span>, <span class="number">4</span>], [<span class="number">5</span>, <span class="number">6</span>]]</span><br><span class="line">print(list(itertools.chain.from_iterable(a_list)))</span><br><span class="line"># Output: [1, 2, 3, 4, 5, 6]</span><br><span class="line">sum(a_list,[])</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]</span><br><span class="line"># or</span><br><span class="line">print(list(itertools.chain(*a_list)))</span><br><span class="line"># Output: [1, 2, 3, 4, 5, 6]</span><br><span class="line">python -c <span class="string">"import csv,json;print json.dumps(list(csv.reader(open('csv_file.csv'))))"</span></span><br><span class="line">python -m cProfile my_script.py</span><br><span class="line">cat file.json | python -m json.tool</span><br><span class="line">https:<span class="comment">//learnku.com/articles/39048#reply125307</span></span><br></pre></td></tr></table></figure>
<h3 id="python算出了同事的身份证号码"><a href="#python算出了同事的身份证号码" class="headerlink" title="python算出了同事的身份证号码"></a>python算出了同事的身份证号码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://mp.weixin.qq.com/s?__biz=MzU5MjI3NzIxMw==&amp;mid=2247486816&amp;idx=1&amp;sn=baa976db515e3b9b99e7001daa9a577a&amp;chksm=fe2376d2c954ffc486625e5420e3ebcf3d83581986b0568b804fb5a54e4aaa032b4992c13905&amp;mpshare=1&amp;scene=1&amp;srcid=1023PX0DRWmDc5E8oEZSVUx6&amp;sharer_sharetime=1571795782903&amp;sharer_shareid=43165518fc08bc947dca48788293333a&amp;key=6f23511bf9e1c01f4c78d4f8f46e1b1e8fc6e548405a6029e3b015de7441c1527cd4817fc238470a3211f36f03178e6f7f9888d5f7d1ee5e6ef6b0b0fced5da2f45aa739e184ae5749a86f5102efd4f9&amp;ascene=1&amp;uin=NjQ3OTQwMTAy&amp;devicetype=Windows+7&amp;version=62070152&amp;lang=zh_CN&amp;pass_ticket=Nl73k%2FpmXYhrLnAbsjSStmagh1FEZZkB8fhtyVf9%2BmzY8foNNpPw%2FmaVHa2zPKdu</span></span><br><span class="line"></span><br><span class="line">用python生成<span class="number">1993</span>年的所有日期吧</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line">#生成出生当年所有日期</span><br><span class="line">def dateRange(year):</span><br><span class="line">    fmt = <span class="string">'%Y-%m-%d'</span></span><br><span class="line">    bgn = int(time.mktime(time.strptime(year+<span class="string">'-01-01'</span>,fmt)))</span><br><span class="line">    end = int(time.mktime(time.strptime(year+<span class="string">'-12-31'</span>,fmt)))</span><br><span class="line">    list_date = [time.strftime(fmt,time.localtime(i)) <span class="keyword">for</span> i <span class="keyword">in</span> range(bgn,end+<span class="number">1</span>,<span class="number">3600</span>*<span class="number">24</span>)]</span><br><span class="line">    <span class="keyword">return</span> [i.replace(<span class="string">'-'</span>,<span class="string">''</span>) <span class="keyword">for</span> i <span class="keyword">in</span> list_date]</span><br><span class="line"></span><br><span class="line">data_time  = dateRange(<span class="string">'1993'</span>)</span><br><span class="line">pip install id-validator</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> id_validator <span class="keyword">import</span> validator</span><br><span class="line"></span><br><span class="line">#遍历所有日期，print通过校验的身份证号码</span><br><span class="line"></span><br><span class="line">def vali_dator(id1,id2,id3):</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> dateRange(id2):</span><br><span class="line">        theid = id1 + i + id3</span><br><span class="line">        <span class="keyword">if</span> validator.is_valid(theid):</span><br><span class="line">            print(theid)</span><br><span class="line"></span><br><span class="line">vali_dator(<span class="string">'330221'</span>,<span class="string">'1993'</span>,<span class="string">'4914'</span>)</span><br><span class="line">打开<span class="number">12306</span>官网，</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">在<span class="number">12306</span>添加常用联系人，</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">将李大伟+身份证号依次输入。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">若身份证和姓名一致，就会显示校验通过；</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">若不能通过，则说明身份证和姓名不一致。 https:<span class="comment">//github.com/zpw1995/aotodata/tree/master/interest/ID_card</span></span><br></pre></td></tr></table></figure>
<h3 id="b站弹幕"><a href="#b站弹幕" class="headerlink" title="b站弹幕"></a>b站弹幕</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line">#https://github.com/zpw1995/aotodata/blob/master/bilibili_danmu/B%E7%AB%99%E5%BC%B9%E5%B9%95%E7%88%AC%E8%99%AB.py</span><br><span class="line">url = <span class="string">'http://comment.bilibili.com/123519261.xml'</span></span><br><span class="line">html = requests.get(url)</span><br><span class="line">html.encoding=<span class="string">'utf8'</span></span><br><span class="line"></span><br><span class="line">soup = BeautifulSoup(html.text, <span class="string">'lxml'</span>)</span><br><span class="line">results = soup.find_all(<span class="string">'d'</span>)</span><br><span class="line"></span><br><span class="line">comments = [comment.text <span class="keyword">for</span> comment <span class="keyword">in</span> results]</span><br><span class="line">comments_dict = &#123;<span class="string">'comments'</span>: comments&#125;</span><br><span class="line"></span><br><span class="line">df = pd.DataFrame(comments_dict)</span><br><span class="line">df.to_csv(<span class="string">'bili_ai5.csv'</span>, encoding=<span class="string">'utf-8-sig'</span>)</span><br></pre></td></tr></table></figure>
<h3 id="Pandas"><a href="#Pandas" class="headerlink" title="Pandas"></a>Pandas</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"> #读取csv</span><br><span class="line"> df = pd.read_csv(<span class="string">'xxx.csv'</span>)</span><br><span class="line"></span><br><span class="line"> #pkl格式https://learnku.com/articles/39739</span><br><span class="line"> df.to_pickle('xxx.pkl') #格式另存</span><br><span class="line"> df = pd.read_pickle('xxx.pkl') #读取</span><br><span class="line"></span><br><span class="line"> #hdf格式</span><br><span class="line">df.to_hdf('xxx.hdf','df') #格式另存</span><br><span class="line">df = pd.read_hdf('xxx.pkl','df') #读取</span><br><span class="line"></span><br><span class="line">boolean=[True,False]</span><br><span class="line">gender=[<span class="string">"男"</span>,<span class="string">"女"</span>]</span><br><span class="line">color=[<span class="string">"white"</span>,<span class="string">"black"</span>,<span class="string">"yellow"</span>]</span><br><span class="line">data=pd.DataFrame(&#123;</span><br><span class="line">    <span class="string">"height"</span>:np.random.randint(<span class="number">150</span>,<span class="number">190</span>,<span class="number">100</span>),</span><br><span class="line">    <span class="string">"weight"</span>:np.random.randint(<span class="number">40</span>,<span class="number">90</span>,<span class="number">100</span>),</span><br><span class="line">    <span class="string">"smoker"</span>:[boolean[x] <span class="keyword">for</span> x <span class="keyword">in</span> np.random.randint(<span class="number">0</span>,<span class="number">2</span>,<span class="number">100</span>)],</span><br><span class="line">    <span class="string">"gender"</span>:[gender[x] <span class="keyword">for</span> x <span class="keyword">in</span> np.random.randint(<span class="number">0</span>,<span class="number">2</span>,<span class="number">100</span>)],</span><br><span class="line">    <span class="string">"age"</span>:np.random.randint(<span class="number">15</span>,<span class="number">90</span>,<span class="number">100</span>),</span><br><span class="line">    <span class="string">"color"</span>:[color[x] <span class="keyword">for</span> x <span class="keyword">in</span> np.random.randint(<span class="number">0</span>,len(color),<span class="number">100</span>) ]</span><br><span class="line">&#125;</span><br><span class="line">)</span><br><span class="line">&gt;&gt;&gt; data</span><br><span class="line">    height  weight  smoker gender  age   color</span><br><span class="line"><span class="number">0</span>      <span class="number">186</span>      <span class="number">77</span>   False      女   <span class="number">59</span>   black</span><br><span class="line"><span class="number">1</span>      <span class="number">162</span>      <span class="number">62</span>   False      女   <span class="number">75</span>  yellow</span><br><span class="line"><span class="number">2</span>      <span class="number">187</span>      <span class="number">78</span>   False      男   <span class="number">66</span>   black</span><br><span class="line"><span class="number">3</span>      <span class="number">166</span>      <span class="number">45</span>    True      男   <span class="number">38</span>   white</span><br><span class="line">#①使用字典进行映射</span><br><span class="line">data[<span class="string">"gender"</span>] = data[<span class="string">"gender"</span>].map(&#123;<span class="string">"男"</span>:<span class="number">1</span>, <span class="string">"女"</span>:<span class="number">0</span>&#125;)</span><br><span class="line"></span><br><span class="line">#②使用函数</span><br><span class="line">def gender_map(x):</span><br><span class="line">    gender = <span class="number">1</span> <span class="keyword">if</span> x == <span class="string">"男"</span> <span class="keyword">else</span> <span class="number">0</span></span><br><span class="line">    <span class="keyword">return</span> gender</span><br><span class="line">#注意这里传入的是函数名，不带括号</span><br><span class="line">data[<span class="string">"gender"</span>] = data[<span class="string">"gender"</span>].map(gender_map)</span><br><span class="line">def apply_age(x,bias):</span><br><span class="line">    <span class="keyword">return</span> x+bias</span><br><span class="line"></span><br><span class="line">#以元组的方式传入额外的参数</span><br><span class="line">data[<span class="string">"age"</span>] = data[<span class="string">"age"</span>].apply(apply_age,args=(<span class="number">-3</span>,))</span><br><span class="line"># 沿着0轴求和</span><br><span class="line">data[[<span class="string">"height"</span>,<span class="string">"weight"</span>,<span class="string">"age"</span>]].apply(np.sum, axis=<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"># 沿着0轴取对数</span><br><span class="line">data[[<span class="string">"height"</span>,<span class="string">"weight"</span>,<span class="string">"age"</span>]].apply(np.log, axis=<span class="number">0</span>)</span><br><span class="line">def BMI(series):</span><br><span class="line">    weight = series[<span class="string">"weight"</span>]</span><br><span class="line">    height = series[<span class="string">"height"</span>]/<span class="number">100</span></span><br><span class="line">    BMI = weight/height**<span class="number">2</span></span><br><span class="line">    <span class="keyword">return</span> BMI</span><br><span class="line"></span><br><span class="line">data[<span class="string">"BMI"</span>] = data.apply(BMI,axis=<span class="number">1</span>)</span><br><span class="line">df.applymap(lambda x:<span class="string">"%.2f"</span> % x)</span><br><span class="line">https:<span class="comment">//learnku.com/articles/39734</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">12</span>]: data.groupby(<span class="string">"company"</span>).agg(<span class="string">'mean'</span>)</span><br><span class="line">Out[<span class="number">12</span>]:</span><br><span class="line">         salary    age</span><br><span class="line">company</span><br><span class="line">A         <span class="number">21.50</span>  <span class="number">27.50</span></span><br><span class="line">B         <span class="number">13.00</span>  <span class="number">29.00</span></span><br><span class="line">C         <span class="number">29.25</span>  <span class="number">27.25</span></span><br><span class="line">In [<span class="number">17</span>]: data.groupby(<span class="string">'company'</span>).agg(&#123;<span class="string">'salary'</span>:<span class="string">'median'</span>,<span class="string">'age'</span>:<span class="string">'mean'</span>&#125;)</span><br><span class="line">Out[<span class="number">17</span>]:</span><br><span class="line">         salary    age</span><br><span class="line">company</span><br><span class="line">A          <span class="number">21.5</span>  <span class="number">27.50</span></span><br><span class="line">B          <span class="number">10.0</span>  <span class="number">29.00</span></span><br><span class="line">C          <span class="number">30.0</span>  <span class="number">27.25</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">24</span>]: data[<span class="string">'avg_salary'</span>] = data.groupby(<span class="string">'company'</span>)[<span class="string">'salary'</span>].transform(<span class="string">'mean'</span>)</span><br><span class="line">https:<span class="comment">//learnku.com/articles/39735</span></span><br></pre></td></tr></table></figure>
<h3 id="requests-抓取网页的通用框架"><a href="#requests-抓取网页的通用框架" class="headerlink" title="requests 抓取网页的通用框架"></a>requests 抓取网页的通用框架</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"># -*- coding: utf-8 -*- </span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"></span><br><span class="line">def getHtmlText(url):</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        response = requests.get(url)</span><br><span class="line">        # 如果状态码不是 200, 则应发 HTTPERROR 异常</span><br><span class="line">        response.raise_for_status()</span><br><span class="line">        # 设置正确的编码方式</span><br><span class="line">        response.encoding = response.apparent_encoding</span><br><span class="line">        <span class="keyword">return</span> response.text</span><br><span class="line">    except:</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"Something Wrong!"</span></span><br><span class="line"></span><br><span class="line">url = <span class="string">'http://www.baidu.com'</span></span><br><span class="line"></span><br><span class="line">result = getHtmlText(url)</span><br><span class="line">print(result)</span><br><span class="line">http:<span class="comment">//www.siya89.com/blog/python%20zero</span></span><br></pre></td></tr></table></figure>
<h3 id="Could-not-fetch-URL"><a href="#Could-not-fetch-URL" class="headerlink" title="Could not fetch URL"></a>Could not fetch URL</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"> pip install -U requests</span><br><span class="line">WARNING: Retrying (Retry(total=<span class="number">4</span>, connect=None, read=None, redirect=None, status=None)) after connection broken by <span class="string">'SSLError(SSLCertVerificationError(1, '</span>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate <span class="keyword">in</span> certificate chain (_ssl.c:<span class="number">1045</span>)<span class="string">'))'</span>: <span class="regexp">/simple/</span>requests/</span><br><span class="line">WARNING: Retrying (Retry(total=<span class="number">3</span>, connect=None, read=None, redirect=None, status=None)) after connection broken by <span class="string">'SSLError(SSLCertVerificationError(1, '</span>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate <span class="keyword">in</span> certificate chain (_ssl.c:<span class="number">1045</span>)<span class="string">'))'</span>: <span class="regexp">/simple/</span>requests/</span><br><span class="line">WARNING: Retrying (Retry(total=<span class="number">2</span>, connect=None, read=None, redirect=None, status=None)) after connection broken by <span class="string">'SSLError(SSLCertVerificationError(1, '</span>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate <span class="keyword">in</span> certificate chain (_ssl.c:<span class="number">1045</span>)<span class="string">'))'</span>: <span class="regexp">/simple/</span>requests/</span><br><span class="line">WARNING: Retrying (Retry(total=<span class="number">1</span>, connect=None, read=None, redirect=None, status=None)) after connection broken by <span class="string">'SSLError(SSLCertVerificationError(1, '</span>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate <span class="keyword">in</span> certificate chain (_ssl.c:<span class="number">1045</span>)<span class="string">'))'</span>: <span class="regexp">/simple/</span>requests/</span><br><span class="line">WARNING: Retrying (Retry(total=<span class="number">0</span>, connect=None, read=None, redirect=None, status=None)) after connection broken by <span class="string">'SSLError(SSLCertVerificationError(1, '</span>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate <span class="keyword">in</span> certificate chain (_ssl.c:<span class="number">1045</span>)<span class="string">'))'</span>: <span class="regexp">/simple/</span>requests/</span><br><span class="line">Could not fetch URL https:<span class="comment">//pypi.org/simple/requests/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/requests/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1045)'))) - skipping</span></span><br><span class="line">Requirement already up-to-date: requests <span class="keyword">in</span> d:\python\lib\site-packages (<span class="number">2.21</span><span class="number">.0</span>)</span><br><span class="line">Requirement already satisfied, skipping upgrade: idna&lt;<span class="number">2.9</span>,&gt;=<span class="number">2.5</span> <span class="keyword">in</span> d:\python\lib\site-packages (<span class="keyword">from</span> requests) (<span class="number">2.8</span>)</span><br><span class="line"></span><br><span class="line"> pip --trusted-host pypi.doubanio.com install -U tqdm -i http:<span class="comment">//pypi.doubanio.com/simple</span></span><br><span class="line">Looking <span class="keyword">in</span> indexes: http:<span class="comment">//pypi.doubanio.com/simple</span></span><br><span class="line">Collecting tqdm</span><br><span class="line">  Downloading http:<span class="comment">//pypi.doubanio.com/packages/4a/1c/6359be64e8301b84160f6f6f7936bbfaaa5e9a4eab6cbc681db07600b949/tqdm-4.45.0-py2.py3-none-any.whl (60kB)</span></span><br><span class="line">     |████████████████████████████████| <span class="number">61</span>kB <span class="number">1.9</span>MB/s</span><br><span class="line">Installing collected packages: tqdm</span><br><span class="line">  Found existing installation: tqdm <span class="number">4.28</span><span class="number">.1</span></span><br><span class="line">    Uninstalling tqdm<span class="number">-4.28</span><span class="number">.1</span>:</span><br><span class="line">      Successfully uninstalled tqdm<span class="number">-4.28</span><span class="number">.1</span></span><br><span class="line">Successfully installed tqdm<span class="number">-4.45</span><span class="number">.0</span></span><br><span class="line"></span><br><span class="line">创建或修改配置文件（linux的文件在~<span class="regexp">/.pip/</span>pip.conf，windows在%HOMEPATH%\pip\pip.ini），修改内容为：</span><br><span class="line"></span><br><span class="line">code:</span><br><span class="line"></span><br><span class="line">[global]</span><br><span class="line"></span><br><span class="line">index-url = http:<span class="comment">//pypi.douban.com/simple</span></span><br></pre></td></tr></table></figure>
<h3 id="Python-Requests-throwing-SSLError"><a href="#Python-Requests-throwing-SSLError" class="headerlink" title="Python Requests throwing SSLError"></a>Python Requests throwing SSLError</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> urllib3</span><br><span class="line"></span><br><span class="line">urllib3.disable_warnings()</span><br><span class="line">pip install certifi</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&gt;&gt;&gt; <span class="keyword">import</span> requests</span><br><span class="line">&gt;&gt;&gt; requests.certs.where()</span><br><span class="line"><span class="string">'D:\\python\\lib\\site-packages\\certifi\\cacert.pem'</span></span><br><span class="line">requests.get(url, verify=False)</span><br><span class="line">cafile = 'cacert.pem' # http://curl.haxx.se/ca/cacert.pem</span><br><span class="line">r = requests.get(url, verify=cafile)</span><br><span class="line"></span><br><span class="line">requests.get(<span class="string">"https://api.github.com/events"</span>, verify=True, cert=[<span class="string">'/path/to/my/ca.crt'</span>])</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//stackoverflow.com/questions/10667960/python-requests-throwing-sslerror</span></span><br><span class="line">https:<span class="comment">//requests.readthedocs.io/en/latest/user/advanced/#ssl-cert-verification</span></span><br></pre></td></tr></table></figure>
<p><a href="https://learnku.com/articles/39732" target="_blank" rel="noopener">Python 数据分析实战 | 用数据带你回顾乔丹的职业生涯</a></p>
<p><a href="https://learnku.com/articles/39640" target="_blank" rel="noopener">Python 包的创建</a></p>
<p><a href="https://learnku.com/python/t/38853" target="_blank" rel="noopener">Flask 和 ChatterBot 构建聊天机器人</a></p>
<p><a href="https://learnku.com/python/t/38748" target="_blank" rel="noopener">小项目来学习 Flask</a></p>
<p><a href="https://learnku.com/python/t/38740" target="_blank" rel="noopener">Flask 项目结构分享</a></p>
<p><a href="https://github.com/hk029/christmas-hat" target="_blank" rel="noopener">纯前端实现人脸识别自动佩戴圣诞帽</a></p>
<p><a href="https://mp.weixin.qq.com/s/_nmpXZY9ctBSnCVdgy0KLA" target="_blank" rel="noopener">python flask 微信机器人</a></p>
<p><a href="https://github.com/Foair/course-crawler" target="_blank" rel="noopener">中国大学MOOC、学堂在线、网易云课堂、好大学在线、爱课程 MOOC 课程下载。</a></p>
<p><a href="https://www.yingjoy.cn/numpy-100-1/" target="_blank" rel="noopener">100个Numpy练习</a></p>
<p><a href="https://learnku.com/articles/36218" target="_blank" rel="noopener">Python 图文识别</a></p>
<p><a href="https://mp.weixin.qq.com/s?__biz=MjM5MTQ4NjA3Nw==&amp;mid=2459679291&amp;idx=1&amp;sn=c5062f89f10f12d9cb2bfb3132f3555f&amp;chksm=b1dbc32986ac4a3f67a09e1afd20337c439b84a81df54c34166d8387b7dce6899091f5a5fa52&amp;mpshare=1&amp;scene=21&amp;srcid=0108BeXqtj8pQ4Fjor6Q5x9i&amp;from=timeline&amp;ascene=2&amp;devicetype=android-19&amp;version=27000038&amp;nettype=WIFI&amp;abtest_cookie=BQABAAoACwASABMAFAAGACOXHgBXmR4Am5keAJ2ZHgCjmR4As5keAAAA&amp;lang=zh_CN&amp;pass_ticket=os0oh%20IZdLx%20aDsZPG7o00lQN1g5c4hQqPsbtpOn4Hie5oswKrr7aOHXbRHTMWMP&amp;wx_header=1#wechat_redirect" target="_blank" rel="noopener">学Python前请你先看看这个文章！</a></p>
<p><a href="https://github.com/eastlakeside/interpy-zh" target="_blank" rel="noopener">Python进阶书籍</a></p>
<p><a href="https://github.com/mozilla/readability" target="_blank" rel="noopener">新闻类网站通用抽取器 https://github.com/kingname/GeneralNewsExtractor </a></p>
<p><a href="https://zhuanlan.zhihu.com/p/70039605" target="_blank" rel="noopener">用Python把公众号文章打包成pdf文件</a></p>
<p><a href="http://jartto.wang/2018/12/31/f2e-2018/" target="_blank" rel="noopener">用 Python 爬取 2018 前端热点</a></p>
<p><a href="https://oicebot.github.io/2018/09/05/30-mins-into-pandas-for-data-science.html" target="_blank" rel="noopener">30分钟带你入门数据分析工具 Pandas</a></p>
<p><a href="http://www.ikeguang.com/2019/08/06/PyCharm2019-register/" target="_blank" rel="noopener">PyCharm2019激活</a></p>
<p><a href="https://me.jinchuang.org/archives/280.html" target="_blank" rel="noopener">wkhtmlpdf命令行工具发送微信图片告警</a></p>
<p><a href="https://post.zz173.com/detail/hfu17Yn_YpMY9TlVNTqv8w.html" target="_blank" rel="noopener">Python解密进相亲交流群</a></p>
<p><a href="https://github.com/sweida/python-study/tree/master/videoToCode" target="_blank" rel="noopener">OpenCV和FFmpeg将普通视频转成代码视频</a></p>
<p><a href="https://python3-cookbook.readthedocs.io/zh_CN/latest/index.html" target="_blank" rel="noopener">python3文档</a></p>
<p><a href="https://learnku.com/articles/31315" target="_blank" rel="noopener">从零开始的 TensorFlow tensorflow.google.cn/</a></p>
<p><a href="https://learnku.com/articles/30542" target="_blank" rel="noopener">Redis 结合 flask 及 vue 实现 SSE 在线聊天</a></p>
<p><a href="http://www.python88.com/user/6732" target="_blank" rel="noopener">Python社区文章</a></p>
<p><a href="http://martinhan.site/2017-04-15/python%E7%89%88-%E6%89%B9%E9%87%8F%E4%B8%AD%E6%96%87%E6%96%87%E4%BB%B6%E5%90%8D%E8%BD%AC%E8%8B%B1%E6%96%87.html" target="_blank" rel="noopener">python版-批量中文文件名转英文</a></p>
<p><a href="https://github.com/xchaoinfo/fuck-login" target="_blank" rel="noopener">模拟登录一些知名的网站，为了方便爬取需要登录的网站fuck login</a></p>
<p><a href="https://finthon.com/data-processing/pandas/" target="_blank" rel="noopener">pandas</a></p>
<p><a href="https://yangfangs.github.io/2018/08/06/python-distribution-packages/" target="_blank" rel="noopener">Python 快速打包发布软件PyPi上</a></p>
<p><a href="https://finthon.com/python-advance/" target="_blank" rel="noopener">Python进阶</a></p>
<p><a href="https://www.restran.net/2015/10/04/supervisord-tutorial/" target="_blank" rel="noopener">Python 进程管理工具 Supervisor 使用教程</a></p>
<p><a href="https://www.restran.net/2017/02/25/python-tutorial-outline/" target="_blank" rel="noopener">Python 入门教程 - 课程大纲</a></p>
<p><a href="">网易云音乐歌曲评论爬虫</a></p>
<p><a href="http://python.jobbole.com/89161/" target="_blank" rel="noopener">爬取豆瓣短评之《后来的我们》</a></p>
<p><a href="https://www.techug.com/post/python-tips-and-trick.html" target="_blank" rel="noopener">python奇技淫巧</a></p>
<p><a href="https://github.com/5alt/ZeroScan" target="_blank" rel="noopener">爆破的方式收集子域名https://5alt.me/tools/</a></p>
<p><a href="https://github.com/CriseLYJ/awesome-python-login-model" target="_blank" rel="noopener">python模拟登陆一些大型网站，还有一些简单的爬虫</a></p>
<p>python 爬虫入门初级篇 <a href="https://piaosanlang.gitbooks.io/spiders/content/01day/README1.html" target="_blank" rel="noopener">https://piaosanlang.gitbooks.io/spiders/content/01day/README1.html</a></p>
<p>python 坦克大战源码 <a href="https://learnku.com/articles/34725" target="_blank" rel="noopener">https://learnku.com/articles/34725</a></p>
<p><a href="https://learnku.com/articles/35053" target="_blank" rel="noopener">pycharm激活码 过期</a></p>
<p><a href="http://www.liujiangblog.com/course/python/1" target="_blank" rel="noopener">python教程</a></p>
<p><a href="https://learnku.com/python/t/35004" target="_blank" rel="noopener">Python 反编译调用有道翻译</a></p>
<p><a href="https://github.com/Bgods/PDF2PNG" target="_blank" rel="noopener">PDF 和图片互转</a></p>
<p><a href="https://learnku.com/articles/35287" target="_blank" rel="noopener">pandas 学习笔记 </a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/python/" rel="tag"># python</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/02/25/微信小程序项目收集/" rel="next" title="微信小程序项目收集">
                <i class="fa fa-chevron-left"></i> 微信小程序项目收集
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/02/28/面试资料收集/" rel="prev" title="面试资料收集">
                面试资料收集 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
          <div class="comments" id="comments">
             <div id="gitment-container"></div>
         </div>
  




        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">苏生不惑</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">382</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">37</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/sushengbuhuo" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mysusheng@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://plus.google.com/mysusheng" target="_blank" title="Google">
                      
                        <i class="fa fa-fw fa-google"></i>Google</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://v2ex.com/" title="v2ex" target="_blank">v2ex</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.fanhaobai.com/" title="fanhaobai" target="_blank">fanhaobai</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="yuanxuxu" target="_blank">yuanxuxu</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.snail-c.cn/article" title="snail-c" target="_blank">snail-c</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://showcj.com/archives" title="showcj" target="_blank">showcj</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://vultr.aicnm.com/%E6%9C%80%E6%96%B0Vultr%E6%B3%A8%E5%86%8C%E5%8F%8AVPS%E8%B4%AD%E4%B9%B0%E5%9B%BE%E6%96%87%E6%95%99%E7%A8%8B/" title="vultr" target="_blank">vultr</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lucissfer.com/" title="lucissfer" target="_blank">lucissfer</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.csdn.net/fdipzone/article/details/79352685" title="傲雪星枫" target="_blank">傲雪星枫</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yoby123.cn/index.php/category/default/" title="小白的分享" target="_blank">小白的分享</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/p/5819995.html" title="PHP攻城狮" target="_blank">PHP攻城狮</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.jiaojie.site/" title="php" target="_blank">php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://sphard.com/archives/" title="sphard" target="_blank">sphard</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="LNMP技术栈笔记" target="_blank">LNMP技术栈笔记</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.coding10.com/" title="学习 Laravel" target="_blank">学习 Laravel</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://shuwoom.com/?page_id=929" title="区块链学习指南" target="_blank">区块链学习指南</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://greenlightt.github.io/archives/" title="greenlightt" target="_blank">greenlightt</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.0php.net/archives/" title="0php" target="_blank">0php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.fordba.com/category/mysql" title="mysql" target="_blank">mysql</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.itcodemonkey.com/" title="程序员" target="_blank">程序员</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yanshuo.me/r/v2ex" title="言说" target="_blank">言说</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.timiguo.com/archive.html" title="提米果的博客" target="_blank">提米果的博客</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://phpartisan.cn/news/112.html" title="phpartisan" target="_blank">phpartisan</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/" title="飞鸿影" target="_blank">飞鸿影</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.54php.cn/" title="54php" target="_blank">54php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lazyman.vip/" title="营销" target="_blank">营销</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.njphper.com/archives/" title="做人呢最重要的就是开心" target="_blank">做人呢最重要的就是开心</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.h57.pw/" title="php 初心者" target="_blank">php 初心者</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-行代码入门-Python-爬虫"><span class="nav-number">1.</span> <span class="nav-text">5 行代码入门 Python 爬虫</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#时间处理"><span class="nav-number">2.</span> <span class="nav-text">时间处理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#error-Microsoft-Visual-C-14-0-is-required"><span class="nav-number">3.</span> <span class="nav-text">error: Microsoft Visual C++ 14.0 is required</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#逗号引发的悲剧"><span class="nav-number">4.</span> <span class="nav-text">逗号引发的悲剧</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python获取Bing图片做壁纸"><span class="nav-number">5.</span> <span class="nav-text">Python获取Bing图片做壁纸</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#自动化测试工具from-selenium-import-webdriver"><span class="nav-number">6.</span> <span class="nav-text">自动化测试工具from selenium import webdriver</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#取交集，并集和差集"><span class="nav-number">7.</span> <span class="nav-text">取交集，并集和差集</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#list分割成固定长度子list"><span class="nav-number">8.</span> <span class="nav-text">list分割成固定长度子list</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#生成英文字母表"><span class="nav-number">9.</span> <span class="nav-text">生成英文字母表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#字典排序"><span class="nav-number">10.</span> <span class="nav-text">字典排序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#微信公众号或网页自动导出"><span class="nav-number">11.</span> <span class="nav-text">微信公众号或网页自动导出</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#批量压缩图片"><span class="nav-number">12.</span> <span class="nav-text">批量压缩图片</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#重试"><span class="nav-number">13.</span> <span class="nav-text">重试</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#登录GitHub"><span class="nav-number">14.</span> <span class="nav-text">登录GitHub</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#多线程和多进程"><span class="nav-number">15.</span> <span class="nav-text">多线程和多进程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Pandas-做数据分析"><span class="nav-number">16.</span> <span class="nav-text">Pandas 做数据分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#多线程"><span class="nav-number">17.</span> <span class="nav-text">多线程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#尾递归"><span class="nav-number">18.</span> <span class="nav-text">尾递归</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取公众号全部文章"><span class="nav-number">19.</span> <span class="nav-text">获取公众号全部文章</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-与-Python-代码对比"><span class="nav-number">20.</span> <span class="nav-text">PHP 与 Python 代码对比</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#python2与3的编码"><span class="nav-number">21.</span> <span class="nav-text">python2与3的编码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Win10-下-Python2-与-Python3-兼容问题"><span class="nav-number">22.</span> <span class="nav-text">Win10 下 Python2 与 Python3 兼容问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#剪刀、石头、布"><span class="nav-number">23.</span> <span class="nav-text">剪刀、石头、布</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#md5和sha1加密"><span class="nav-number">24.</span> <span class="nav-text">md5和sha1加密</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#concat-组合-dataframe"><span class="nav-number">25.</span> <span class="nav-text">concat 组合 dataframe</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数学问题"><span class="nav-number">26.</span> <span class="nav-text">数学问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python语法速查"><span class="nav-number">27.</span> <span class="nav-text">Python语法速查</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#bing搜索"><span class="nav-number">28.</span> <span class="nav-text">bing搜索</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#requests-html"><span class="nav-number">29.</span> <span class="nav-text">requests-html</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#输出今天日期"><span class="nav-number">30.</span> <span class="nav-text">输出今天日期</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#保存json文件"><span class="nav-number">31.</span> <span class="nav-text">保存json文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#openCV"><span class="nav-number">32.</span> <span class="nav-text">openCV</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#图片转pdf"><span class="nav-number">33.</span> <span class="nav-text">图片转pdf</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#模拟登录"><span class="nav-number">34.</span> <span class="nav-text">模拟登录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#猫眼票房"><span class="nav-number">35.</span> <span class="nav-text">猫眼票房</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#锟斤拷"><span class="nav-number">36.</span> <span class="nav-text">锟斤拷</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#安装scrapy失败"><span class="nav-number">37.</span> <span class="nav-text">安装scrapy失败</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ChromeDriver"><span class="nav-number">38.</span> <span class="nav-text">ChromeDriver</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#生成彩色动态二维码"><span class="nav-number">39.</span> <span class="nav-text">生成彩色动态二维码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#爬取简书用户的动态"><span class="nav-number">40.</span> <span class="nav-text">爬取简书用户的动态</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#retrying-重试请求"><span class="nav-number">41.</span> <span class="nav-text">retrying 重试请求</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ipython"><span class="nav-number">42.</span> <span class="nav-text">ipython</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#列表中出现次数最多的数"><span class="nav-number">43.</span> <span class="nav-text">列表中出现次数最多的数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#pygame-模块"><span class="nav-number">44.</span> <span class="nav-text">pygame 模块</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#发送html格式邮件"><span class="nav-number">45.</span> <span class="nav-text">发送html格式邮件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#pdf"><span class="nav-number">46.</span> <span class="nav-text">pdf</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#天善博客内容如何转成PDF文档"><span class="nav-number">47.</span> <span class="nav-text">天善博客内容如何转成PDF文档</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#如何批量添加图片水印"><span class="nav-number">48.</span> <span class="nav-text">如何批量添加图片水印</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#爬虫简书"><span class="nav-number">49.</span> <span class="nav-text">爬虫简书</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#markdown-转PDF"><span class="nav-number">50.</span> <span class="nav-text">markdown 转PDF</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#验证身份证号"><span class="nav-number">51.</span> <span class="nav-text">验证身份证号</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#NumPy-基础"><span class="nav-number">52.</span> <span class="nav-text">NumPy 基础</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#把bmp和png转换成jpg"><span class="nav-number">53.</span> <span class="nav-text">把bmp和png转换成jpg</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#numpy"><span class="nav-number">54.</span> <span class="nav-text">numpy</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#pdf-to-image"><span class="nav-number">55.</span> <span class="nav-text">pdf to image</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#图片exif"><span class="nav-number">56.</span> <span class="nav-text">图片exif</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#命令行里处理数据科学问题"><span class="nav-number">57.</span> <span class="nav-text">命令行里处理数据科学问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Pandas-做数据分析sql"><span class="nav-number">58.</span> <span class="nav-text">Pandas 做数据分析sql</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#在-Python-里用-is-进行比较"><span class="nav-number">59.</span> <span class="nav-text">在 Python 里用 is 进行比较</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#判断括号字符串"><span class="nav-number">60.</span> <span class="nav-text">判断括号字符串</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Could-not-fetch-URL-https-pypi-org-simple-pip-There-was-a-problem-confirming-the-ssl-certificate"><span class="nav-number">61.</span> <span class="nav-text">Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#单行代码"><span class="nav-number">62.</span> <span class="nav-text">单行代码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#python算出了同事的身份证号码"><span class="nav-number">63.</span> <span class="nav-text">python算出了同事的身份证号码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#b站弹幕"><span class="nav-number">64.</span> <span class="nav-text">b站弹幕</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Pandas"><span class="nav-number">65.</span> <span class="nav-text">Pandas</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#requests-抓取网页的通用框架"><span class="nav-number">66.</span> <span class="nav-text">requests 抓取网页的通用框架</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Could-not-fetch-URL"><span class="nav-number">67.</span> <span class="nav-text">Could not fetch URL</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-Requests-throwing-SSLError"><span class="nav-number">68.</span> <span class="nav-text">Python Requests throwing SSLError</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">苏生不惑</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>



<div>
<span id="showDays"></span>

</div>

<span id="busuanzi_container_site_pv">
   总访问量:<span id="busuanzi_value_site_pv"></span>次
</span>



  <span class="post-meta-divider">|</span>



<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共773.8k字</span>
</div>
<script>
var birthDay = new Date("11/20/2018");
var now = new Date();
var duration = now.getTime() - birthDay.getTime();
var total= Math.floor(duration / (1000 * 60 * 60 * 24));
document.getElementById("showDays").innerHTML = "本站已运行 "+total+" 天";
</script>

        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  











<script type="text/javascript">
    (function() {
        // 匿名函数，防止污染全局变量
        var utterances = document.createElement('script');
        utterances.type = 'text/javascript';
        utterances.async = true;
        utterances.setAttribute('issue-term','0')
        utterances.setAttribute('theme','')
        utterances.setAttribute('repo','sushengbuhuo/laravel_ioc_demo')
        utterances.crossorigin = 'anonymous';
        utterances.src = 'https://utteranc.es/client.js';
        // content 是要插入评论的地方
        document.getElementById('gitment-container').appendChild(utterances);
    })();
</script>


  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

  
<script>
  ((window.gitter = {}).chat = {}).options = {
    //room替换成自己的聊天室名称即可，room的名称规则是：username/roomname
    room: 'sushengbuhuo-chat/mychat'
  };
</script>
<script src="https://sidecar.gitter.im/dist/sidecar.v1.js" async defer></script>

</body>
</html>
